[英]Get all columns in a pandas dataframe that is a date-column when different time-zones are present in different columns
(Note, this SO question does not take into account the different time-zones ie it does not catch a date when a time-zone is present). (注意, 这个SO 问题没有考虑到不同的时区,即它没有捕捉到存在时区的日期)。
I have a dataframe where some column dtypes are datetime64[ns]
and some are datetime64[ns,UTC]
.我有一个 dataframe ,其中一些列 dtypes 是
datetime64[ns]
,有些是datetime64[ns,UTC]
。
Note, they have all been converted using请注意,它们都已使用
df["some_time_col"] = pd.to_datetime(df["some_time_col"])
but since df
is a merge of data across different databases, the time-zones are different. df["some_time_col"] = pd.to_datetime(df["some_time_col"])
但由于df
是跨不同数据库的数据合并,因此时区不同。
Is there a way to get all columns that are a date-column ie something like this有没有办法让所有列都是日期列,即像这样
dt_cols = df.select_dtypes(include=[pd.datetime]).columns
instead of having to specify all time-zones like而不必指定所有时区,如
dt_cols = df.select_dtypes(include=["datetim64","datetime64[UTC]","datetime64[UTC+1]"...).columns
Using either of使用任何一个
dt_cols = df.select_dtypes(include=[np.datetime64]).columns
dt_cols = df.select_dtypes(include=["datetime64"]).columns
does not catch datetime64[ns,UTC]
but only datetime64[ns]
不捕捉
datetime64[ns,UTC]
但只捕捉 datetime64 datetime64[ns]
Use:利用:
df.select_dtypes('datetimetz')
From select_dtypes
docs:从
select_dtypes
文档:
To select Pandas datetimetz dtypes, use 'datetimetz' (new in 0.20.0) or 'datetime64[ns, tz]'
对于 select Pandas datetimetz dtypes,使用 'datetimetz'(0.20.0 中的新功能)或 'datetime64[ns, tz]'
For selecting both datetime
without tz and with tz, do:要选择不带 tz 和带 tz 的
datetime
时间,请执行以下操作:
df.select_dtypes(['datetimetz', 'np.datetime64'])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.