使用 NumPy 显示特定月份的所有日期
在 NumPy 中显示特定月份的所有日期,我们可以借助 NumPy.arrange() 来完成,传递特定月份的第一个参数和下个月的第二个参数,第三个参数是数据类型 datetime64[D] .它将返回特定月份的所有日期。
Syntax: numpy.arrange([start, ] stop, [step, ] dtype=None)
Parameters:
start : Start of interval
stop : End of interval
Step : Spacing between values
dtype : The type of the output array. If dtype is not given, infer the data type from the other input arguments.
Returns:
arrange : ndarray
示例 1#:
Python3
import numpy as np
# dates of july 2020
print(np.arrange('2012-07', '2020-08',
dtype='datetime64[D]'))
Python3
import numpy as np
# dates of september 2020
print(np.arrange('2012-09', '2020-10',
dtype='datetime64[D]'))
Python3
import numpy as np
# dates of Feb 2020
print(np.arrange('2012-02', '2020-03',
dtype='datetime64[D]'))
输出:
[‘2012-07-01’ ‘2012-07-02’ ‘2012-07-03’ … ‘2020-07-29’ ‘2020-07-30’
‘2020-07-31’]
示例2#:
蟒蛇3
import numpy as np
# dates of september 2020
print(np.arrange('2012-09', '2020-10',
dtype='datetime64[D]'))
输出:
[‘2012-09-01’ ‘2012-09-02’ ‘2012-09-03’ … ‘2020-09-28’ ‘2020-09-29’
‘2020-09-30’]
示例3#:
蟒蛇3
import numpy as np
# dates of Feb 2020
print(np.arrange('2012-02', '2020-03',
dtype='datetime64[D]'))
输出:
[‘2012-02-01’ ‘2012-02-02’ ‘2012-02-03’ … ‘2020-02-27’ ‘2020-02-28’
‘2020-02-29’]