如何在 python 中从特定的 ISO 格式正确解析日期

2023-12-10

我连接到数据库上的一个表,其中有两列带日期。 我可以毫无问题地解析具有如下格式的值的列:2017-11-03
但我找不到一种方法来解析日期格式如下的另一列:2017-10-03 05:06:52.840 +02:00

我的尝试
如果我通过解析单个值strptime method

dt.datetime.strptime("2017-12-14 22:16:24.037 +02:00", "%Y-%m-%d %H:%M:%S.%f %z")

我得到了正确的输出
datetime.datetime(2017, 12, 14, 22, 16, 24, 37000, tzinfo=datetime.timezone(datetime.timedelta(seconds=7200)))

但是如果我在将表解析为数据帧时尝试使用相同的代码格式,则列 dtype 是一个对象:

Licenze_FromGY = pd.read_sql(query, cnxn, parse_dates={"EndDate":"%Y-%m-%d", "LastUpd":"%Y-%m-%d %H:%M:%S.%f %z"})


<class 'pandas.core.frame.DataFrame'>
RangeIndex: 1000 entries, 0 to 999
Data columns (total 10 columns):
 #   Column     Non-Null Count  Dtype         
---  ------     --------------  -----         
 0   Tenant     1000 non-null   int64         
 1   IdService  1000 non-null   object        
 2   Code       1000 non-null   object        
 3   Aggregate  1000 non-null   object        
 4   Bundle     991 non-null    object        
 5   Status     1000 non-null   object        
 6   Value      1000 non-null   int64         
 7   EndDate    258 non-null    datetime64[ns]
 8   Trial      1000 non-null   bool          
 9   LastUpd    1000 non-null   object 

我还尝试更改代码格式read_sql方法或在pd.to_datetime()方法,但随后所有值都变成NaT:

Licenze_FromGY["LastUpd"] = pd.to_datetime(Licenze_FromGY["LastUpd"], format="%Y-%m-%d %H:%M:%S.%fZ", errors="coerce") 

<class 'pandas.core.frame.DataFrame'>
RangeIndex: 1000 entries, 0 to 999
Data columns (total 10 columns):
 #   Column     Non-Null Count  Dtype         
---  ------     --------------  -----         
 0   Tenant     1000 non-null   int64         
 1   IdService  1000 non-null   object        
 2   Code       1000 non-null   object        
 3   Aggregate  1000 non-null   object        
 4   Bundle     991 non-null    object        
 5   Status     1000 non-null   object        
 6   Value      1000 non-null   int64         
 7   EndDate    258 non-null    datetime64[ns]
 8   Trial      1000 non-null   bool          
 9   LastUpd    0 non-null      datetime64[ns]
dtypes: bool(1), datetime64[ns](2), int64(2), object(5)
memory usage: 71.4+ KB
None 

有人可以帮忙吗?


pandas 无法处理一个系列(列)中的混合 UTC 偏移量。假设你有这样的数据

import pandas as pd

df = pd.DataFrame({"datetime": ["2017-12-14 22:16:24.037 +02:00", 
                                "2018-08-14 22:16:24.037 +03:00"]})

如果你只是解析日期时间,

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

df["datetime"]
0    2017-12-14 22:16:24.037000+02:00
1    2018-08-14 22:16:24.037000+03:00
Name: datetime, dtype: object

你得到 dtype 对象。该系列的元素属于 Python datetime.datetime dtype。与 pandas 日期时间数据类型相比,这限制了日期时间功能。

你可以得到这个,例如通过解析为 UTC:

df["datetime"] = pd.to_datetime(df["datetime"], utc=True)

df["datetime"]
0   2017-12-14 20:16:24.037000+00:00
1   2018-08-14 19:16:24.037000+00:00
Name: datetime, dtype: datetime64[ns, UTC]

您可以设置适当的时区来重新创建 UTC 偏移量:

df["datetime"] = pd.to_datetime(df["datetime"], utc=True).dt.tz_convert("Europe/Athens")

df["datetime"]
0   2017-12-14 22:16:24.037000+02:00
1   2018-08-14 22:16:24.037000+03:00
Name: datetime, dtype: datetime64[ns, Europe/Athens]
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在 python 中从特定的 ISO 格式正确解析日期 的相关文章

随机推荐