如何从 ISO 8601 日期时间格式获取小时-分钟-秒? [英] How to get hours-minute-seconds from ISO 8601 date time format?

查看:42
本文介绍了如何从 ISO 8601 日期时间格式获取小时-分钟-秒?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 Pandas 中处理一个 Excel 文件,我试图在其中处理日期列,其中日期以 ISO 8601 格式列出.我想取这一列并将日期和时间存储在两个不同的列中.这两列中的值需要存储在东部夏令时中.他们应该是这样的

I am working with an Excel file in Pandas where I am trying to deal with a Date column where the Date is listed in ISO 8601 format. I want to take this column and store the date and time in two different columns.The values in these two columns need to be stored in Eastern Daylight Savings. This is what they are supposed to look like

Date                    Date (New)  Time (New)
1999-01-01T00:00:29.75  12/31/1998  6:59:58 PM
1999-01-01T00:00:30.00  12/31/1998  6:59:59 PM
1999-01-01T00:00:32.25  12/31/1998  7:00:00 PM
1999-01-01T00:00:30.50  12/31/1998  6:59:58 PM

我已经部分实现了这一点.我已将这些值转换为东部夏令时并成功正确存储了日期值.但是,我希望时间值以 12 小时格式存储,而不是像现在这样以 24 小时格式存储?

I have achieved this, partially. I have converted the values to Eastern Daylight savings time and successfully stored the Date value correctly. However, I want the time value to be stored in the 12 hours format and not in the 24 hours format as it is being right now?

这是我目前输出的样子.

This is what my output looks like so far.

Date                  Date (New)    Time (New)
1999-01-01T00:00:29.75  1998-12-31  19:00:30
1999-01-01T00:00:30.00  1998-12-31  19:00:30
1999-01-01T00:00:32.25  1998-12-31  19:00:32
1999-01-01T00:00:30.50  1998-12-31  19:00:31

有人知道我可以为此做什么吗?

Does anyone have any idea what i can do for this?

from pytz import timezone

import dateutil.parser

from pytz import UTC

import datetime as dt

df3['Day']=pd.to_datetime(df['Date'], format='%Y-%m-%d %H:%M:    %S.%f',errors='coerce').dt.tz_localize('UTC')

df3['Day']= df3['Day'].dt.tz_convert('US/Eastern')
df3['Date(New)'], df3['Time(New)'] = zip(*[(d.date(), d.time()) for d in  df3['Day']])

推荐答案

您可以设置用于输出的时间格式 - 时间值本身(并且应该)存储为 datetime.time() - 如果你想要一个特定的字符串表示,你可以以你想要的格式创建一个字符串类型的列:

You can set the time format used for outputting - the time value itself is (and should be) stored as datetime.time() - if you want a specific string representation you can create a string-type column in the format you want:

from pytz import timezone 
import pandas as pd
import datetime as dt

df= pd.DataFrame([{"Date":dt.datetime.now()}]) 

df['Day']=pd.to_datetime( df['Date'], format='%Y-%m-%d %H:%M:    %S.%f',
                          errors='coerce').dt.tz_localize('UTC')

df['Day']= df['Day'].dt.tz_convert('US/Eastern')
df['Date(New)'], df['Time(New)'] = zip(*[(d.date(), d.time()) for d in  df['Day']]) 

# create strings with specific formatting
df['Date(asstring)'] = df['Day'].dt.strftime("%Y-%m-%d")
df['Time(asstring)'] = df["Day"].dt.strftime("%I:%M:%S %p") 

# show resulting column / cell types
print(df.dtypes)
print(df.applymap(type))
# show df
print(df)

输出:

# df.dtypes
Date                          datetime64[ns]
Day               datetime64[ns, US/Eastern]
Date(New)                             object
Time(New)                             object
Date(asstring)                        object
Time(asstring)                        object

# from df.applymap(type)
Date            <class 'pandas._libs.tslib.Timestamp'>
Day             <class 'pandas._libs.tslib.Timestamp'>  
Date(New)       <class 'datetime.date'>
Time(New)       <class 'datetime.time'>
Date(asstring)  <class 'str'>
Time(asstring)  <class 'str'>

# from print(df)
                        Date                              Day   Date(New)        Time(New) 
0 2019-01-04 00:40:02.802606 2019-01-03 19:40:02.802606-05:00  2019-01-03  19:40:02.802606 

Date(asstring) Time(asstring)
    2019-01-03    07:40:02 PM

这篇关于如何从 ISO 8601 日期时间格式获取小时-分钟-秒?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆