大熊猫numpy系列天数变为1

时间:2017-02-23 07:36:38

标签: pandas datetime numpy series

我有一个时间序列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   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]')))
奇怪的是,我得到了:

>>> unique_days
Out[9]: array(['2016-05-31', '2016-06-01', '2016-06-02', '2016-06-03', '2016-06-05', '2016-06-06', '2016-06-07', '2016-06-08', '2016-06-09', '2016-06-10', '2016-06-12', '2016-06-13', '2016-06-14', '2016-06-15', '2016-06-16', '2016-06-17', '2016-06-19', '2016-06-20', '2016-06-21', '2016-06-22', '2016-06-23', '2016-06-24', '2016-06-26', '2016-06-27', '2016-06-28', '2016-06-29', '2016-06-30', '2016-07-01', '2016-07-03', '2016-07-04', '2016-07-05', '2016-07-06', '2016-07-07', '2016-07-08', '2016-07-10', '2016-07-11', '2016-07-12', '2016-07-13', '2016-07-14', '2016-07-15', '2016-07-17', '2016-07-18', '2016-07-19', '2016-07-20', '2016-07-21', '2016-07-22', '2016-07-24', '2016-07-25', '2016-07-26'], dtype='datetime64[D]')

所以基本上它将天数换了1天。有没有办法解决这个问题以获得正确的日子?

2 个答案:

答案 0 :(得分:1)

我认为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)

对我来说,rounddays unique合作{/ 1}}。

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)

答案 1 :(得分:0)

使用np.unique(np.array(x.index.values.astype('<M8[D]')))

而不是row_number()

为我解决了这个问题。