Numpy datetime64 提取月-日
Numpy datetime64 extract month-day
我有 3d 数据集,我只想有月份日期,即 numpy.datetime64('05-09')。
知道怎么做吗?原始 3d 数据集:
<xarray.DataArray (time: 153, lat: 281, lon: 481)>
array([[[5839.5454, 5839.581 , 5839.4844, ..., 5865.13 , 5865.8213,
5865.933 ],
[5838.614 , 5838.59 , 5838.548 , ..., 5864.121 , 5864.8354,
5865.13 ],
[[5853.7334, 5854.075 , 5854.291 , ..., 5882.3706, 5882.638 ,
5882.379 ],
[5853.297 , 5853.583 , 5853.858 , ..., 5882.602 , 5882.878 ,
5882.687 ],
...,
[5230.2534, 5230.2534, 5230.2534, ..., 5230.2534, 5230.2534,
5230.2534]]], dtype=float32)
Coordinates:
* lon (lon) float64 -80.0 -79.75 -79.5 -79.25 ... 39.25 39.5 39.75 40.0
* lat (lat) float64 20.0 20.25 20.5 20.75 21.0 ... 89.25 89.5 89.75 90.0
* time (time) datetime64[ns] 2000-05-01 2000-05-02 ... 2000-09-30
作为一个选项,我可以建议从您的数组制作一个数据框。将数据框更改为字符串 one,然后提取必要的元素。将数据变成列表。
但是,它将是一种字符串格式。如果你把它转换回 datetime64,你会得到另一个东西。
import numpy
import pandas as pd
aaa = [numpy.datetime64('2000-05-01T00:00:00.000000000'),
numpy.datetime64('2000-05-02T00:00:00.000000000'),
numpy.datetime64('2000-05-03T00:00:00.000000000'),
numpy.datetime64('2000-05-04T00:00:00.000000000'),
numpy.datetime64('2000-05-05T00:00:00.000000000'),
numpy.datetime64('2000-05-06T00:00:00.000000000'),
numpy.datetime64('2000-05-07T00:00:00.000000000'),
numpy.datetime64('2000-05-08T00:00:00.000000000'),
numpy.datetime64('2000-05-09T00:00:00.000000000')]
df = pd.DataFrame(aaa, columns=['abc'])
df['abc'] = df['abc'].astype('str').str[5:10]
bbb = df['abc'].to_list()
输出
['05-01', '05-02', '05-03', '05-04', '05-05', '05-06', '05-07', '05-08', '12-01']
如果你对matplotlib库中的绘图感到满意。然后在代码中我将它设置为 'formatter' 以便只有月份和日期显示在 x-axis.
import matplotlib.pyplot as plt
import matplotlib.dates
import numpy
import pandas as pd
aaa = [numpy.datetime64('2000-05-01T00:00:00.000000000'),
numpy.datetime64('2000-05-02T00:00:00.000000000'),
numpy.datetime64('2000-05-03T00:00:00.000000000'),
numpy.datetime64('2000-05-04T00:00:00.000000000'),
numpy.datetime64('2000-05-05T00:00:00.000000000'),
numpy.datetime64('2000-05-06T00:00:00.000000000'),
numpy.datetime64('2000-05-07T00:00:00.000000000'),
numpy.datetime64('2000-05-08T00:00:00.000000000'),
numpy.datetime64('2000-05-09T00:00:00.000000000')]
ttt = [1, 3, 7, 5, 10, 2, 5, 1, 7]
fig, ax = plt.subplots()
ax.plot(aaa, ttt)
ax.xaxis.set_major_formatter(matplotlib.dates.DateFormatter("%m.%d"))
fig.autofmt_xdate()
plt.show()
您只需访问 dt
attribute:
import numpy as np
import pandas as pd
first = np.datetime64('2000-05-01T00:00:00.000000000')
data = [first + np.timedelta64(i, 'D') for i in range(10)]
df = pd.DataFrame(data, columns=['full'])
df['Y'] = df['full'].dt.year
df['M'] = df['full'].dt.month
df['D'] = df['full'].dt.day
print(df)
# full Y M D
# 0 2000-05-01 2000 5 1
# 1 2000-05-02 2000 5 2
# 2 2000-05-03 2000 5 3
# 3 2000-05-04 2000 5 4
# 4 2000-05-05 2000 5 5
# 5 2000-05-06 2000 5 6
# 6 2000-05-07 2000 5 7
# 7 2000-05-08 2000 5 8
# 8 2000-05-09 2000 5 9
# 9 2000-05-10 2000 5 10
我有 3d 数据集,我只想有月份日期,即 numpy.datetime64('05-09')。 知道怎么做吗?原始 3d 数据集:
<xarray.DataArray (time: 153, lat: 281, lon: 481)>
array([[[5839.5454, 5839.581 , 5839.4844, ..., 5865.13 , 5865.8213,
5865.933 ],
[5838.614 , 5838.59 , 5838.548 , ..., 5864.121 , 5864.8354,
5865.13 ],
[[5853.7334, 5854.075 , 5854.291 , ..., 5882.3706, 5882.638 ,
5882.379 ],
[5853.297 , 5853.583 , 5853.858 , ..., 5882.602 , 5882.878 ,
5882.687 ],
...,
[5230.2534, 5230.2534, 5230.2534, ..., 5230.2534, 5230.2534,
5230.2534]]], dtype=float32)
Coordinates:
* lon (lon) float64 -80.0 -79.75 -79.5 -79.25 ... 39.25 39.5 39.75 40.0
* lat (lat) float64 20.0 20.25 20.5 20.75 21.0 ... 89.25 89.5 89.75 90.0
* time (time) datetime64[ns] 2000-05-01 2000-05-02 ... 2000-09-30
作为一个选项,我可以建议从您的数组制作一个数据框。将数据框更改为字符串 one,然后提取必要的元素。将数据变成列表。 但是,它将是一种字符串格式。如果你把它转换回 datetime64,你会得到另一个东西。
import numpy
import pandas as pd
aaa = [numpy.datetime64('2000-05-01T00:00:00.000000000'),
numpy.datetime64('2000-05-02T00:00:00.000000000'),
numpy.datetime64('2000-05-03T00:00:00.000000000'),
numpy.datetime64('2000-05-04T00:00:00.000000000'),
numpy.datetime64('2000-05-05T00:00:00.000000000'),
numpy.datetime64('2000-05-06T00:00:00.000000000'),
numpy.datetime64('2000-05-07T00:00:00.000000000'),
numpy.datetime64('2000-05-08T00:00:00.000000000'),
numpy.datetime64('2000-05-09T00:00:00.000000000')]
df = pd.DataFrame(aaa, columns=['abc'])
df['abc'] = df['abc'].astype('str').str[5:10]
bbb = df['abc'].to_list()
输出
['05-01', '05-02', '05-03', '05-04', '05-05', '05-06', '05-07', '05-08', '12-01']
如果你对matplotlib库中的绘图感到满意。然后在代码中我将它设置为 'formatter' 以便只有月份和日期显示在 x-axis.
import matplotlib.pyplot as plt
import matplotlib.dates
import numpy
import pandas as pd
aaa = [numpy.datetime64('2000-05-01T00:00:00.000000000'),
numpy.datetime64('2000-05-02T00:00:00.000000000'),
numpy.datetime64('2000-05-03T00:00:00.000000000'),
numpy.datetime64('2000-05-04T00:00:00.000000000'),
numpy.datetime64('2000-05-05T00:00:00.000000000'),
numpy.datetime64('2000-05-06T00:00:00.000000000'),
numpy.datetime64('2000-05-07T00:00:00.000000000'),
numpy.datetime64('2000-05-08T00:00:00.000000000'),
numpy.datetime64('2000-05-09T00:00:00.000000000')]
ttt = [1, 3, 7, 5, 10, 2, 5, 1, 7]
fig, ax = plt.subplots()
ax.plot(aaa, ttt)
ax.xaxis.set_major_formatter(matplotlib.dates.DateFormatter("%m.%d"))
fig.autofmt_xdate()
plt.show()
您只需访问 dt
attribute:
import numpy as np
import pandas as pd
first = np.datetime64('2000-05-01T00:00:00.000000000')
data = [first + np.timedelta64(i, 'D') for i in range(10)]
df = pd.DataFrame(data, columns=['full'])
df['Y'] = df['full'].dt.year
df['M'] = df['full'].dt.month
df['D'] = df['full'].dt.day
print(df)
# full Y M D
# 0 2000-05-01 2000 5 1
# 1 2000-05-02 2000 5 2
# 2 2000-05-03 2000 5 3
# 3 2000-05-04 2000 5 4
# 4 2000-05-05 2000 5 5
# 5 2000-05-06 2000 5 6
# 6 2000-05-07 2000 5 7
# 7 2000-05-08 2000 5 8
# 8 2000-05-09 2000 5 9
# 9 2000-05-10 2000 5 10