我有一个 Pandas DatetimeIndex
对象(从数据帧的索引获得),我需要每个元素的工作日。我一直在 Windows 上使用 Python 3.6.5(64 位)和 Pandas 0.22.0 运行此程序并获取 Int64Index 对象:
例如
In:
ts = pd.DatetimeIndex([['2013-01-01 00:00:00', '2013-01-01 00:30:00',
'2013-01-02 01:00:00', '2013-01-02 01:30:00',
'2013-01-03 02:00:00', '2013-01-03 02:30:00',
'2013-01-04 03:00:00', '2013-01-04 03:30:00',
'2013-01-05 04:00:00', '2013-01-05 04:30:00']])
ts.weekday
Out:
Int64Index([1, 1, 2, 2, 3, 3, 4, 4, 5, 5], dtype='int64')
我现在正在 pythonanywhere
上运行脚本,该脚本具有 Python 3.6.0
和 Pandas 0.19.2
,结果作为数组:
In:
ts = pd.DatetimeIndex(['2013-01-01 00:00:00', '2013-01-01 00:30:00',
'2013-01-02 01:00:00', '2013-01-02 01:30:00',
'2013-01-03 02:00:00', '2013-01-03 02:30:00',
'2013-01-04 03:00:00', '2013-01-04 03:30:00',
'2013-01-05 04:00:00', '2013-01-05 04:30:00'])
ts.weekday
Out:
array([1, 1, 2, 2, 3, 3, 4, 4, 5, 5], dtype=int32)
是否可以编写此代码以在两个平台上返回相同的结果(最好是 Index
对象)?
最佳答案
此行为在最近的更新中已更改,以返回 pd.Index
而不是数组。为了向后兼容,您始终可以根据需要将结果转换为 Int64Index
。
>>> pd.Index(ts.weekday, dtype=np.int64)
Int64Index([1, 1, 2, 2, 3, 3, 4, 4, 5, 5], dtype='int64')
关于python - Pandas DatetimeIndex.weekday 应该返回索引还是 numpy 数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50902204/