我正在使用date = df.index.values
将我的时间序列按3天的频率进行分组。要检索时间数组,我使用array(['2010-01-31T00:00:00.000000000', '2010-02-03T00:00:00.000000000',
'2017-05-12T00:00:00.000000000', '2017-05-15T00:00:00.000000000',
'2017-05-18T00:00:00.000000000', '2017-05-21T00:00:00.000000000',
'2017-05-24T00:00:00.000000000', '2017-05-27T00:00:00.000000000',
'2017-05-30T00:00:00.000000000', '2017-06-02T00:00:00.000000000',
'2017-06-05T00:00:00.000000000', '2017-06-08T00:00:00.000000000',
'2017-06-11T00:00:00.000000000', '2017-06-14T00:00:00.000000000',
'2017-06-17T00:00:00.000000000', '2017-06-20T00:00:00.000000000',
'2017-06-23T00:00:00.000000000', '2017-06-26T00:00:00.000000000',
'2017-06-29T00:00:00.000000000', '2017-07-02T00:00:00.000000000',
'2017-07-05T00:00:00.000000000', '2017-07-08T00:00:00.000000000',
'2017-07-11T00:00:00.000000000', '2017-07-14T00:00:00.000000000',
'2017-07-17T00:00:00.000000000', '2017-07-20T00:00:00.000000000',
'2017-07-23T00:00:00.000000000', '2017-07-26T00:00:00.000000000',
'2017-07-29T00:00:00.000000000', '2017-08-01T00:00:00.000000000',
'2017-08-04T00:00:00.000000000', '2017-08-07T00:00:00.000000000'],
dtype='datetime64[ns]')
会返回一个时间数组,如下所示:>
times =['2010-02-03T00:00:00.000000000','2010-02-03T00:00:00.000000000']
t = Time(times, format='isot', scale='utc')
print(t.mjd)
>>[55230. 55230.]
我一直在尝试获取日期(最后是MJD)。当我复制此数组的1-2个元素并执行此操作时,它将起作用;
from astropy.time import Time
t = Time(date, format='isot', scale='utc')
print(t.mjd)
但是,我无法在整个数组中使用相同类型的代码
() => {}
它给我一个错误“ 输入值与isot格式类不匹配”。因此,我猜想Time将需要列表而不是数组,但是将Date更改为list并不能解决问题。我无法解决,上面的示例是2个字符串的列表,它工作正常。我在这里做错了什么?我尝试了使用熊猫的其他几种方法并尝试遍历元素。谢谢您的帮助。 阿比
答案 0 :(得分:1)
自从astropy 3.1起,就内置了对datetime64
的支持,因此您可以轻松地做到这一点:
In [2]: dates = np.array(['2010-01-31T00:00:00', '2010-02-03T00:00:00'],
...: dtype='datetime64[ns]')
...:
In [3]: tm = Time(dates)
In [4]: tm.mjd
Out[4]: array([55227., 55230.])
答案 1 :(得分:-1)
在查看了this link
之后,找到了一种解决方法from astropy.time import Time
date = df.index.values
a= []
for i in [x for x in date]:
ts = pd.to_datetime(str(i))
d = ts.strftime('%Y-%m-%d')
a.append(d)
print(d)
grouped_date = Time(a, format='iso', out_subfmt='date')
grouped_date_mjd = grouped_date.mjd
print(a[0:3], grouped_date_mjd[0:3])
>> ['2010-01-31', '2010-02-03', '2010-02-06'] [55227. 55230. 55233.]