简体   繁体   English

如何根据两列日期和时间在 pandas 中创建日期时间列?

[英]How to create a datetime column in pandas based on two columns date and time?

Lets say I have following DataFrame :可以说我有以下DataFrame

import pandas as pd
df = pd.DataFrame({"Date":["2022-01-01", "2022-01-02", "2022-01-03"], "Time":[0,1,10]})

I want another column which is a datetime using the Date and the Time Columns.我想要另一列是使用datetime和时间列的日期时间。

Expected result预期结果

         Date  Time            DateTime
0  2022-01-01     0 2022-01-01 00:00:00
1  2022-01-02     1 2022-01-02 01:00:00
2  2022-01-03    10 2022-01-03 10:00:00

Trial审判

I tried this failed solution:我尝试了这个失败的解决方案:

df["DateTime"] = str(df["Date"]) + " " + str(df["Time"]) + ":00"

Which outputs:哪些输出:

>>> df
         Date  Time                                           DateTime
0  2022-01-01     0  0    2022-01-01\n1    2022-01-02\n2    2022-01...
1  2022-01-02     1  0    2022-01-01\n1    2022-01-02\n2    2022-01...
2  2022-01-03    10  0    2022-01-01\n1    2022-01-02\n2    2022-01...

You can just do Date and Time add with to_datetime and to_timedelta您可以使用to_datetimeto_timedelta添加DateTime

df['new'] = pd.to_datetime(df['Date']) + pd.to_timedelta(df['Time'],unit='hour')
df
Out[388]: 
         Date  Time                 new
0  2022-01-01     0 2022-01-01 00:00:00
1  2022-01-02     1 2022-01-02 01:00:00
2  2022-01-03    10 2022-01-03 10:00:00

One solution would be the following:一种解决方案如下:

df["DateTime"] = df["Date"].astype(str) + " " + df["Time"].astype(str) + ":00"

Then convert to datetime :然后转换为datetime时间:

df["DateTime"] = pd.to_datetime(df["DateTime"])

Hence the output is as expected:因此 output 符合预期:

>>> df
         Date  Time            DateTime
0  2022-01-01     0 2022-01-01 00:00:00
1  2022-01-02     1 2022-01-02 01:00:00
2  2022-01-03    10 2022-01-03 10:00:00
df['date'] = [d.date() for d in df['datetime']]
df['time'] = [d.time() for d in df['datetime']]
df['date']= pd.to_datetime(df['date'])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 pandas - 将时间和日期从两个 dataframe 列组合到一个日期时间列 - pandas - combine time and date from two dataframe columns to a datetime column 熊猫根据其他两个具有日期时间值的列创建一个布尔列 - pandas create a boolean column based on two other columns with datetime values 如何将csv文件中的两列日期和时间合并到pandas中的1个datetime列? - How do I combine two columns of date and time in a csv file to 1 datetime column in pandas? 如何基于比较两列 pandas 的按位异或创建列 - How to create a column based of the bitwise XOR of comparing two columns pandas 如何根据Pandas中条件的现有列创建两列? - How create two columns based on existing column with conditions in Pandas? Python pandas - 将日期和时间列加入带有时区的日期时间列 - Python pandas - join date & time columns into datetime column with timezone Pandas:将日期和时间列作为一个日期时间列的 read_csv - Pandas: read_csv with date and time columns as one datetime column 使用熊猫从3列中的日期/时间信息创建索引日期时间 - Create an indexed datetime from date/time info in 3 columns using pandas Pandas数据框:根据日期列比较创建其他列 - Pandas dataframe: Create additional column based on date columns comparison 如何获得两个日期时间列熊猫之间的时间差? - How to get difference of time between two datetime columns pandas?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM