【问题标题】:pandas numpy series days getting shifted by 1pandas numpy 系列天数变了 1
【发布时间】:2017-07-13 13:47:49
【问题描述】:

我有一个时间序列 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 天。有没有办法解决这个问题?

【问题讨论】:

    标签: pandas datetime numpy series


    【解决方案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)
    

    对我来说,rounddaysunique 一起工作:

    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)
    

    【讨论】:

    • 感谢 jezrael,这很有帮助。但是,现在我有一个更简单的方法来处理它发布在下面。
    【解决方案2】:

    使用unique_days = np.unique(x.index.date)

    而不是np.unique(np.array(x.index.values.astype('&lt;M8[D]')))

    为我解决了这个问题。

    【讨论】:

      猜你喜欢
      • 2019-01-19
      • 2019-05-05
      • 2017-09-30
      • 2019-01-08
      • 2015-12-01
      • 2018-01-30
      • 1970-01-01
      • 1970-01-01
      • 2022-01-24
      相关资源
      最近更新 更多