将index转换为datetime并获得Column 2的最大时间

时间:2018-04-05 07:21:28

标签: python pandas

我的数据框看起来像这样:

0    203
1    250
2    318
3    786
4    321
5    135

我正在尝试将索引列转换为日期时间并保留第二列。

pq = pq.index.to_datetime()

输出:

DatetimeIndex([          '1970-01-01 00:00:00',
               '1970-01-01 00:00:00.000000001',
               '1970-01-01 00:00:00.000000002',
               ...]

这只是从索引创建一个日期时间数据框,不是吗?

1)我如何替换'具有日期时间的索引,同时仍保留数据帧的其余部分?

2)datetime的pd.DataFrame.idxmax()相当于什么?

编辑:我办公室的熊猫版本是0.18.1

Edit2:一位同事帮我解决了这个问题;

date = pd.date_range('2017-01-01', end = '2017-12-31 23:45', freq = '15min')
pq.index = date

获取第2列的最大时间:

pq.idxmax()

2 个答案:

答案 0 :(得分:3)

我认为需要to_datetime

pq.index = pd.to_datetime(pq.index)
print (pq)
                               col
1970-01-01 00:00:00.000000000  203
1970-01-01 00:00:00.000000001  250
1970-01-01 00:00:00.000000002  318
1970-01-01 00:00:00.000000003  786
1970-01-01 00:00:00.000000004  321
1970-01-01 00:00:00.000000005  135

还有一个很好的参数origin用于定义开始日期时间(pandas 0.20.0 +):

pq.index = pd.to_datetime(pq.index, origin=pd.Timestamp('2000-01-01'), unit='d')   
print (pq)
            col
2000-01-01  203
2000-01-02  250
2000-01-03  318
2000-01-04  786
2000-01-05  321
2000-01-06  135

要检查最大索引,可以使用Series.idxmax

print (pq['col'].idxmax())
2000-01-04 00:00:00

编辑:

对于较低版本,可以使用:

pq.index = pd.Timestamp('2000-01-01') + pd.to_timedelta(pq.index, unit='d')   
print (pq)
            col
2000-01-01  203
2000-01-02  250
2000-01-03  318
2000-01-04  786
2000-01-05  321
2000-01-06  135

答案 1 :(得分:1)

如果您想将日期时间作为索引,可以

pq.set_index(pd.to_datetime(pq.index), inplace=True, drop=True)
相关问题