[英]pandas to_datetime from two text columns
I have the following time-series of the format below.我有以下格式的以下时间序列。
What's the easiest way to convert/combine columns 'date' and 'time' into a pandas datetime format?将“日期”和“时间”列转换/组合成熊猫日期时间格式的最简单方法是什么?
I know it should be pandas.to_datetime(date...).我知道它应该是 pandas.to_datetime(date...)。 But I can't figure out the format to combine them.
但我无法弄清楚将它们组合起来的格式。
ccy date time open high low close
0 EURUSD 20190211 100 1.1318 1.1319 1.1317 1.1319
1 EURUSD 20190211 200 1.1320 1.1322 1.1319 1.1319
2 EURUSD 20190211 300 1.1318 1.1319 1.1318 1.1319
3 EURUSD 20190211 400 1.1319 1.1319 1.1318 1.1318
4 EURUSD 20190211 500 1.1318 1.1318 1.1318 1.1318
This is how you would do the conversion, but you need to make sure the time column makes sense - your table is ambiguous right now when displaying 3 digits for time.这是您进行转换的方式,但您需要确保时间列有意义 - 当显示 3 位时间时,您的表格现在不明确。 Also, make sure your date and time columns are strings, else convert them to strings because joining them sensibly will require it.
此外,请确保您的日期和时间列是字符串,否则将它们转换为字符串,因为明智地加入它们将需要它。
import pandas as pd
df=pd.DataFrame({'date':['20190211','20190211'],'time':['0100','0200']})
pd.to_datetime(df['date'] + df['time'], format='%Y%d%m%H%M')
It seems like date
and time
are currently stored as integers.似乎
date
和time
当前存储为整数。 Therefore you might need to cast them as strings in order to perform string slicing.因此,您可能需要将它们转换为字符串才能执行字符串切片。
def convert_to_datetime(date, time):
datestr = str(date)
timestr = str(time)
result = datestr[0:4] + "/" + datestr[4:6] + "/" + datestr[6:]
result = result + " " + timestr[0:-2] + ":"+ timestr[-2:]
return pd.to_datetime(result)
df["datetime"] = df.apply(lambda x: convert_to_datetime(x["date"], x["time"], axis=1)
Two ways to do it:有两种方法可以做到:
# convert date
data['ddate']= [str(dt)[:4]+'-'+str(dt)[4:6]+'-'+str(dt)[-2:] for dt in
# convert time
data['dtime']= [str(dt)[:-2]+':'+str(dt)[-2:] for dt in data['dtime']]
# join
data['datetime'] = data['ddate'] + ' ' + data['dtime'] # space or whatever you want
# delete unneeded columns
del data['ddate'], data['dtime']
data['datetime']= pd.to_datetime([str(dt)+str(tm) for dt,tm in data[['date','time']].values],format='%Y%d%m%H%M')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.