简体   繁体   English

如何将年、月和日列组合为单个日期时间列?

[英]How to combine year, month, and day columns to single datetime column?

I have the following dataframe df :我有以下数据框df

        id  lat        lon      year    month   day         
0       381 53.30660   -0.54649 2004    1       2       
1       381 53.30660   -0.54649 2004    1       3            
2       381 53.30660   -0.54649 2004    1       4   

and I want to create a new column df['Date'] where the year , month , and day columns are combined according to the format yyyy-md .我想创建一个新列df['Date'] ,其中yearmonthday列根据格式yyyy-md

Following this post , I did:这篇文章之后,我做了:

`df['Date']=pd.to_datetime(df['year']*10000000000
                           +df['month']*100000000
                           +df['day']*1000000,
                           format='%Y-%m-%d%')`

The result is not what I expected, as it starts from 1970 instead of 2004, and it also contains the hour stamp, which I did not specify:结果不是我所期望的,因为它是从 1970 年而不是 2004 年开始的,并且它还包含我没有指定的小时戳:

        id  lat        lon      year    month   day  Date           
0       381 53.30660   -0.54649 2004    1       2    1970-01-01 05:34:00.102    
1       381 53.30660   -0.54649 2004    1       3    1970-01-01 05:34:00.103         
2       381 53.30660   -0.54649 2004    1       4    1970-01-01 05:34:00.104

As the dates should be in the 2004-1-2 format, what am I doing wrong?由于日期应该是2004-1-2格式,我做错了什么?

There is an easier way:有一个更简单的方法:

In [250]: df['Date']=pd.to_datetime(df[['year','month','day']])

In [251]: df
Out[251]:
    id      lat      lon  year  month  day       Date
0  381  53.3066 -0.54649  2004      1    2 2004-01-02
1  381  53.3066 -0.54649  2004      1    3 2004-01-03
2  381  53.3066 -0.54649  2004      1    4 2004-01-04

from docs :来自文档

Assembling a datetime from multiple columns of a DataFrame.从 DataFrame 的多列组装日期时间。 The keys can be common abbreviations like [ year , month , day , minute , second , ms , us , ns ]) or plurals of the same键可以是常见的缩写,如 [ yearmonthdayminutesecondmsusns ]) 或相同的复数形式

One solution would be to convert these columns to string, concatenate using agg + str.join , and then convert to datetime .一种解决方案是将这些列转换为字符串,使用agg + str.join连接,然后转换为datetime

df['Date'] = pd.to_datetime(
    df[['year', 'month', 'day']].astype(str).agg('-'.join, axis=1))

df

    id      lat      lon  year  month  day       Date
0  381  53.3066 -0.54649  2004      1    2 2004-01-02
1  381  53.3066 -0.54649  2004      1    3 2004-01-03
2  381  53.3066 -0.54649  2004      1    4 2004-01-04

You may also want to add an errors='coerce' argument if you have invalid datetime combinations between your columns.如果列之间的日期时间组合无效,您可能还想添加一个errors='coerce'参数。

To fix your code修复您的代码

df['Date']=pd.to_datetime(df.year*10000+df.month*100+df.day,format='%Y%m%d')
df
Out[57]: 
    id      lat      lon  year  month  day       Date
0  381  53.3066 -0.54649  2004      1    2 2004-01-02
1  381  53.3066 -0.54649  2004      1    3 2004-01-03
2  381  53.3066 -0.54649  2004      1    4 2004-01-04

I struggled to find a solution because I was working with a dataset with columns in Spanish.我努力寻找解决方案,因为我正在处理一个包含西班牙语列的数据集。 As soon as I translated them to "year" "month" and "day" and "hour", the conversion worked perfectl一旦我将它们翻译成“年”“月”“日”和“小时”,转换就完美了

暂无
暂无

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

相关问题 如何将年(2位),月和日的3个单独列合并为单个日期列 - How to combine 3 separate columns of year(2 digit) ,month and day into single date column 如何将年、月、日、小时/分钟列转换为单个日期时间列? - How to convert year, month, day, hour/minute columns into a single datetime column? 如何为 Pandas 中的 2 天数据从年、日、小时和分钟列(无月列)创建日期时间 object? - How to create a datetime object from Year, Day, Hour and Minute columns (without month column) for 2 day data in Pandas? 使用pandas将年份和月份列干净地组合到单个日期列 - Cleanly combine year and month columns to single date column with pandas 如何从月份和年份列创建日期时间索引,但没有日期? - How to create datetime index from month and year columns, but without day? 如何使用python将日期列拆分为日/月/年列 - How to split a date column into day/month/year columns using python 如何将日-时格式的2列合并成年-月-日-时格式? - How to combine 2 columns in a day-hour format and convert them into a year-month-day-hour format? 结合月份和年份列来创建日期列 - Combine month and year columns to create date column 根据月份和月份创建日期和日期列 - Create datetime column from month and day with year based on month SQLAlchemy - 使用DateTime列查询按月/日/年进行筛选 - SQLAlchemy - Querying with DateTime columns to filter by month/day/year
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM