[英]Python pandas - join date & time columns into datetime column with timezone
[英]Pandas: Join Date and Time into one datetime column
我有一個包含 13 列的數據框,其中兩列是“日期”和“時間”。
我想 1) 加入“日期”和“時間”和 2) 正確格式化結果 (DD.MM.YYYY HH:MM:SS)
Date Time
0 2012-09-27 00:40:20
1 2012-09-27 07:24:37
2 2012-09-27 07:25:08
3 2012-09-27 07:43:48
4 2012-09-27 07:44:27
5 2012-09-27 07:55:16
6 2012-09-27 08:04:19
7 2012-09-27 08:08:21
8 2012-09-27 08:09:08
9 2012-09-27 08:15:30
10 2012-09-27 08:15:35
所以我嘗試:
>type(df["Date"])
pandas.core.series.Series
我想“為什么不將它們都轉換為字符串表示並以這種方式操作它們?”
>g = df["Time"].astype(str)
>type(g)
pandas.core.series.Series
“什么?!還是系列?!”
所以我嘗試發揮創意:
>f = df["Date"].astype(str) + " " + df["Time"].astype(str)
>f
0 2012-09-27 00:40:20
1 2012-09-27 07:24:37
2 2012-09-27 07:25:08
3 2012-09-27 07:43:48
“嘿!那行得通!不,讓我們把格式弄對(DD.MM.YYYY HH:MM:SS)”
>#dt.strptime(f, '%Y-%m-%d %H:%M:%S')
TypeError: strptime() argument 1 must be str, not Series
“還是一個系列……嗯……顯然它不會讓自己被轉換。我們試試別的東西吧”
>f = p.concat([df["Date"], df["Time"]], axis=1)
>f
Date Time
0 2012-09-27 00:40:20
1 2012-09-27 07:24:37
2 2012-09-27 07:25:08
“不錯!但是……這兩個柱子還不是……?”
>f.shape
(100,2)
“對了……回到一樓……”
我沒有想法了......
任何人?
可以將列連接並分配給數據框中的新列或現有列:
df['datetime'] = df['dates'] + " " + df['time']
輸出示例:
dates time datetime
0 2012-09-27 00:40:20 2012-09-27 00:40:20
1 2012-09-27 07:24:37 2012-09-27 07:24:37
2 2012-09-27 07:25:08 2012-09-27 07:25:08
我認為您需要使用to_datetime
加入列:
print (type(df.loc[0, 'Date']))
<class 'str'>
print (type(df.loc[0, 'Time']))
<class 'str'>
df['datetime'] = pd.to_datetime(df['Date'] + " " + df['Time'])
如果有string
s 的日期時間:
print (type(df.loc[0, 'Date']))
<class 'pandas._libs.tslib.Timestamp'>
print (type(df.loc[0, 'Time']))
<class 'str'>
df['datetime'] = df['Date'] + pd.to_timedelta(df['Time'])
如果有time
s 的日期time
:
print (type(df.loc[0, 'Date']))
<class 'pandas._libs.tslib.Timestamp'>
print (type(df.loc[0, 'Time']))
<class 'datetime.time'>
df['datetime'] = df['Date'] + pd.to_timedelta(df['Time'].astype(str))
如果有帶時間的日期:
print (type(df.loc[0, 'Date']))
<class 'datetime.date'>
print (type(df.loc[0, 'Time']))
<class 'datetime.time'>
df['datetime'] = pd.to_datetime(df["Date"].astype(str) + " " + df["Time"].astype(str))
print (df)
Date Time datetime
0 2012-09-27 00:40:20 2012-09-27 00:40:20
1 2012-09-27 07:24:37 2012-09-27 07:24:37
2 2012-09-27 07:25:08 2012-09-27 07:25:08
3 2012-09-27 07:43:48 2012-09-27 07:43:48
4 2012-09-27 07:44:27 2012-09-27 07:44:27
5 2012-09-27 07:55:16 2012-09-27 07:55:16
6 2012-09-27 08:04:19 2012-09-27 08:04:19
7 2012-09-27 08:08:21 2012-09-27 08:08:21
8 2012-09-27 08:09:08 2012-09-27 08:09:08
9 2012-09-27 08:15:30 2012-09-27 08:15:30
10 2012-09-27 08:15:35 2012-09-27 08:15:35
.astype(str)
適用於 Series 的元素,而不是 Series 本身,所以當然type(df["Time"].astype(str)) == pd.Series
。 這似乎是您大部分困惑的根源,您正在對系列采取行動,而不是其元素。
一個解決方案(可能有更簡單的方法)是循環遍歷系列:
dts = [datetime.datetime.strptime(elem, '%Y-%m-%d%H:%M:%S')
for elem in df['Date'] + df['Time']]
fmted = [elem.strftime('%d-%m-%Y %H:%M:%S') for elem in dts]
df.insert(0, 'DateTime', fmted)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.