[英]Parse object index with date, time, and time zone
Python Q. How to parse an object index in a data frame into its date, time, and time zone? Python 问:如何将数据框中的 object 索引解析为其日期、时间和时区?
The format is "YYY-MM-DD HH:MM:SS-HH:MM" where the right "HH:MM" is the timezone.格式为“YYY-MM-DD HH:MM:SS-HH:MM”,其中右侧的“HH:MM”是时区。
Example: Midnight Jan 1st, 2020 in Mountain Time:示例:山区时间 2020 年 1 月 1 日午夜:
2020-01-01 00:00:00-07:00 2020-01-01 00:00:00-07:00
I'm trying to convert this into seven columns in the data frame:我正在尝试将其转换为数据框中的七列:
YYYY, MM, DD, HH, MM, SS, TZ YYYY、MM、DD、HH、MM、SS、TZ
Use pd.to_datetime
to parse a string column into a datetime array使用
pd.to_datetime
将字符串列解析为日期时间数组
datetimes = pd.to_datetime(column)
once you have this, you can access elements of the datetime object with the .dt
datetime accessor :一旦你有了这个,你可以使用
.dt
日期时间访问器访问日期时间 object 的元素:
final = pd.DataFrame({
"year": datetimes.dt.year,
"month": datetimes.dt.month,
"day": datetimes.dt.day,
"hour": datetimes.dt.hour,
"minute": datetimes.dt.minute,
"second": datetimes.dt.second,
"timezone": datetimes.dt.tz,
})
See the pandas user guide section on date/time functionality for more info有关详细信息,请参阅 pandas 用户指南部分的日期/时间功能
df
Date
0 2022-05-01 01:10:04+07:00
1 2022-05-02 05:09:10+07:00
2 2022-05-02 11:22:05+07:00
3 2022-05-02 10:00:30+07:00
df['Date'] = pd.to_datetime(df['Date'])
df['tz']= df['Date'].dt.tz
df['year']= df['Date'].dt.year
df['month']= df['Date'].dt.month
df['month_n']= df['Date'].dt.month_name()
df['day']= df['Date'].dt.day
df['day_n']= df['Date'].dt.day_name()
df['h']= df['Date'].dt.hour
df['mn']= df['Date'].dt.minute
df['s']= df['Date'].dt.second
df['T']= df['Date'].dt.time
df['D']= df['Date'].dt.date
Date tz year month month_n day day_n h mn s T D
0 2022-05-01 01:10:04+07:00 pytz.FixedOffset(420) 2022 5 May 1 Sunday 1 10 4 01:10:04 2022-05-01
1 2022-05-02 05:09:10+07:00 pytz.FixedOffset(420) 2022 5 May 2 Monday 5 9 10 05:09:10 2022-05-02
2 2022-05-02 11:22:05+07:00 pytz.FixedOffset(420) 2022 5 May 2 Monday 11 22 5 11:22:05 2022-05-02
3 2022-05-02 10:00:30+07:00 pytz.FixedOffset(420) 2022 5 May 2 Monday 10 0 30 10:00:30 2022-05-02
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.