2

(Note, this SO question does not take into account the different time-zones i.e it does not catch a date when a time-zone is present).

I have a dataframe where some column dtypes are datetime64[ns] and some are 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.

Is there a way to get all columns that are a date-column i.e 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]

CutePoison
  • 4,679
  • 5
  • 28
  • 63

1 Answers1

3

Use:

df.select_dtypes('datetimetz')

From select_dtypes docs:

To select Pandas datetimetz dtypes, use 'datetimetz' (new in 0.20.0) or 'datetime64[ns, tz]'

For selecting both datetime without tz and with tz, do:

df.select_dtypes(['datetimetz', 'np.datetime64'])
Mayank Porwal
  • 33,470
  • 8
  • 37
  • 58
  • 1
    how did I miss that ... Although it does ignore the `datetime64[ns]` it seems like i.e it only takes columns that have a time-zone thus I need to do `df.select_dtypes(["datetime64","datetimetz"]).columns`. Isn't there a "meta" datetime object that captures *all* date-columns? – CutePoison Dec 03 '21 at 12:42
  • Seems like there's no superset kinda function that catches both datetime with and without tz cols. – Mayank Porwal Dec 03 '21 at 12:51
  • I cannot find it either - so I think we have to include both – CutePoison Dec 03 '21 at 12:57