Pandas Year-Month格式到时间戳

时间:2016-03-12 17:01:12

标签: pandas timestamp time-series

我有像这样的时间索引的数据框

2015-01
2015-02
2015-03
2015-04
2015-05

现在我想将它们转换为时间戳,就像这样

2015-01-31 00:00:00
2015-02-28 00:00:00
2015-03-31 00:00:00
2015-04-30 00:00:00
2015-05-31 00:00:00

如何转换它们?我试过这样的事情,但它需要几天我没有。

import pandas as pd
import datetime as dt
df.index = [dt.datetime(d.year, d.month) for d in df.index]
`TypeError`: Required argument 'day' (pos 3) not found

非常感谢你!

2 个答案:

答案 0 :(得分:0)

我认为您可以先尝试转换to_datetime,然后再添加MonthEnd()

print df
         a
2015-01  1
2015-02  2
2015-03  2
2015-04  3
2015-05  4

print df.index
PeriodIndex(['2015-01', '2015-02', '2015-03', '2015-04', '2015-05'], dtype='int64', freq='M')

df.index = df.index.to_datetime()
print df.index
DatetimeIndex(['2015-01-01', '2015-02-01', '2015-03-01', '2015-04-01',
               '2015-05-01'],
              dtype='datetime64[ns]', freq='MS')

df.index = df.index + pd.offsets.MonthEnd()
print df.index
DatetimeIndex(['2015-01-31', '2015-02-28', '2015-03-31', '2015-04-30',
               '2015-05-31'],
              dtype='datetime64[ns]', freq='M')

print df
            a
2015-01-31  1
2015-02-28  2
2015-03-31  2
2015-04-30  3
2015-05-31  4

答案 1 :(得分:0)

您确定要时间为00:00:00吗?那将是午夜,在该月的倒数第二天和最后一天之间

months = pd.Series(['2015-01', '2015-02', '2015-03', '2015-04', '2015-05'])
>>> [pd.Period(m, 'M').end_time for m in months]

[Timestamp('2015-01-31 23:59:59.999999999'),
 Timestamp('2015-02-28 23:59:59.999999999'),
 Timestamp('2015-03-31 23:59:59.999999999'),
 Timestamp('2015-04-30 23:59:59.999999999'),
 Timestamp('2015-05-31 23:59:59.999999999')]