简体   繁体   English

获取 pandas dataframe 中的所有列,当不同列中存在不同时区时,该列是日期列

[英]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.

相关问题 如何获取Python中同一列或不同列的日期列和最大日期之间的日差? - How to get the day difference between date-column and maximum date of same column or different column in Python? Pandas计算数据帧中不同列上的所有匹配项 - Pandas count all occurrences on different columns in a dataframe 如何获取pandas中不同列的日期格式 - How to get the date format of different Columns in pandas 合并两个不同列中存在的日期以生成列的平均值 - Combine date present in two different columns to generate mean for a column 从 pandas dataframe 列中的列表中分离 dict 到不同的 dataframe 列 - separate dict from list in pandas dataframe column into different dataframe columns 对 Pandas Dataframe 中不同列的多索引 - Multiindex to different columns in Pandas Dataframe 将 2 dataframe 与 pandas 中的不同列合并 - merging 2 dataframe with different columns in pandas 在熊猫数据框中选择不同的列 - Selecting different columns in a pandas dataframe Pandas Dataframe 更新列基于将其他一些列与另一个具有不同列数的 dataframe 的列进行比较 - Pandas Dataframe updating a column based comparing some other columns with the columns of another dataframe with different number of columns pandas - 将时间和日期从两个 dataframe 列组合到一个日期时间列 - pandas - combine time and date from two dataframe columns to a datetime column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM