我有一个 Pandas 数据框:
lat lng alt days date time
0 40.003834 116.321462 211 39745.175405 2008-10-24 04:12:35
1 40.003783 116.321431 201 39745.175463 2008-10-24 04:12:40
2 40.003690 116.321429 203 39745.175521 2008-10-24 04:12:45
3 40.003589 116.321427 194 39745.175579 2008-10-24 04:12:50
4 40.003522 116.321412 190 39745.175637 2008-10-24 04:12:55
5 40.003509 116.321484 188 39745.175694 2008-10-24 04:13:00
为此,我正在尝试将df ['date']和df ['time']列转换为日期时间。我可以:
df['Datetime'] = pd.to_datetime(df['date']+df['time'])
df = df.set_index(['Datetime'])
del df['date']
del df['time']
我得到:
lat lng alt days
Datetime
2008-10-2404:12:35 40.003834 116.321462 211 39745.175405
2008-10-2404:12:40 40.003783 116.321431 201 39745.175463
2008-10-2404:12:45 40.003690 116.321429 203 39745.175521
2008-10-2404:12:50 40.003589 116.321427 194 39745.175579
2008-10-2404:12:55 40.003522 116.321412 190 39745.175637
但是,如果我尝试:
df.between_time(time(1),time(22,59,59))['lng'].std()
我收到一个错误-“TypeError:索引必须为DatetimeIndex”
因此,我也尝试设置DatetimeIndex:
df['Datetime'] = pd.to_datetime(df['date']+df['time'])
#df = df.set_index(['Datetime'])
df = df.set_index(pd.DatetimeIndex(df['Datetime']))
del df['date']
del df['time']
这也会引发错误-'DateParseError:未知的字符串格式'
如何正确创建datetime列和DatetimeIndex,以便df.between_time()正常工作?
最佳答案
为了简化Kirubaharan's answer:
df['Datetime'] = pd.to_datetime(df['date'] + ' ' + df['time'])
df = df.set_index('Datetime')
并删除不需要的列(如OP所做的,但本身未在问题中指定):df = df.drop(['date','time'], axis=1)