Pandas 大熊猫numpy系列活动天数减少1天
我有一个具有以下结构的timeseries x:Pandas 大熊猫numpy系列活动天数减少1天,pandas,datetime,numpy,series,Pandas,Datetime,Numpy,Series,我有一个具有以下结构的timeseries x: >>> type(x) Out[5]: pandas.core.series.Series >>> x.head() Out[6]: 2016-06-01 00:00:00+09:00 110.946 2016-06-01 00:01:00+09:00 110.887 2016-06-01 00:02:00+09:00 110.864 2016-06-01 00:03:00+09:00 11
>>> type(x)
Out[5]: pandas.core.series.Series
>>> x.head()
Out[6]:
2016-06-01 00:00:00+09:00 110.946
2016-06-01 00:01:00+09:00 110.887
2016-06-01 00:02:00+09:00 110.864
2016-06-01 00:03:00+09:00 110.877
2016-06-01 00:04:00+09:00 110.904
>>> x.tail()
Out[7]:
2016-07-27 08:55:00+09:00 104.905
2016-07-27 08:56:00+09:00 104.865
2016-07-27 08:57:00+09:00 104.875
2016-07-27 08:58:00+09:00 104.855
2016-07-27 08:59:00+09:00 104.845
>>> x.index
Out[8]:
DatetimeIndex(['2016-06-01 00:00:00+09:00', '2016-06-01 00:01:00+09:00', '2016-06-01 00:02:00+09:00', '2016-06-01 00:03:00+09:00', '2016-06-01 00:04:00+09:00', '2016-06-01 00:05:00+09:00', '2016-06-01 00:06:00+09:00', '2016-06-01 00:07:00+09:00', '2016-06-01 00:08:00+09:00', '2016-06-01 00:09:00+09:00',
...
'2016-07-27 08:50:00+09:00', '2016-07-27 08:51:00+09:00', '2016-07-27 08:52:00+09:00', '2016-07-27 08:53:00+09:00', '2016-07-27 08:54:00+09:00', '2016-07-27 08:55:00+09:00', '2016-07-27 08:56:00+09:00', '2016-07-27 08:57:00+09:00', '2016-07-27 08:58:00+09:00', '2016-07-27 08:59:00+09:00'], dtype='datetime64[ns]', length=55364, freq=None, tz='Asia/Tokyo')
现在,如果我尝试使用以下代码获取x中唯一的天数:
unique_days = np.unique(np.array(x.index.values.astype('<M8[D]')))
所以基本上,它将一天换一天。有没有办法获得正确的日期?我认为numpy中的时区有问题-它会转换为tz-aware
DatetimeIndex
print (x.index.tz_convert(None))
DatetimeIndex(['2016-05-31 15:00:00', '2016-05-31 15:01:00',
'2016-05-31 15:02:00', '2016-05-31 15:03:00',
'2016-05-31 15:04:00'],
dtype='datetime64[ns]', name='idx', freq=None)
对于我来说,使用unique
,从轮到天
:
print (x.index.round('D').unique())
print (x.index)
DatetimeIndex(['2016-06-01 00:00:00+09:00', '2016-06-01 00:01:00+09:00',
'2016-06-01 00:02:00+09:00', '2016-06-01 00:03:00+09:00',
'2016-06-01 00:04:00+09:00'],
dtype='datetime64[ns, Asia/Tokyo]', name='idx', freq=None)
print (x.index.round('D').unique())
DatetimeIndex(['2016-06-01 00:00:00+09:00'],
dtype='datetime64[ns, Asia/Tokyo]', name='idx', freq=None)
使用unique\u days=np.unique(x.index.date)
而不是np.unique(np.array(x.index.values.astype)('谢谢jezrael,这很有帮助。不过,现在我在下面有一个更简单的方法来处理它。
print (x.index.round('D').unique())
print (x.index)
DatetimeIndex(['2016-06-01 00:00:00+09:00', '2016-06-01 00:01:00+09:00',
'2016-06-01 00:02:00+09:00', '2016-06-01 00:03:00+09:00',
'2016-06-01 00:04:00+09:00'],
dtype='datetime64[ns, Asia/Tokyo]', name='idx', freq=None)
print (x.index.round('D').unique())
DatetimeIndex(['2016-06-01 00:00:00+09:00'],
dtype='datetime64[ns, Asia/Tokyo]', name='idx', freq=None)