简体   繁体   中英

Selecting datetime column names in pandas dataframe

I read an excel file using pandas read_excel with parse_dates=True . Below are the columns of the dataframe:

df_source.columns:

Index([          'Country',    'Cost Component',            'Source',
       'Internal/External',          2019-01-01,          2019-02-01,
                2019-03-01,          2019-04-01,          2019-05-01,
                2019-06-01,          2019-07-01,          2019-08-01,
                2019-09-01,          2019-10-01,          2019-11-01,
                2019-12-01,          2020-01-01,          2020-02-01,
                2020-03-01,          2020-04-01,          2020-05-01,
                2020-06-01,          2020-07-01,          2020-08-01,
                2020-09-01,          2020-10-01,          2020-11-01,
                2020-12-01,          2021-01-01,          2021-02-01,
                2021-03-01,          2021-04-01,          2021-05-01,
                2021-06-01,          2021-07-01,          2021-08-01,
                2021-09-01,          2021-10-01,          2021-11-01,
                2021-12-01,          2022-01-01,          2022-02-01,
                2022-03-01,          2022-04-01,          2022-05-01,
                2022-06-01,          2022-07-01,          2022-08-01,
                2022-09-01,          2022-10-01,          2022-11-01,
                2022-12-01,          2023-01-01,          2023-02-01,
                2023-03-01,          2023-04-01,          2023-05-01,
                2023-06-01,          2023-07-01,          2023-08-01,
                2023-09-01,          2023-10-01,          2023-11-01,
                2023-12-01,          2024-01-01,          2024-02-01,
                2024-03-01,          2024-04-01,          2024-05-01,
                2024-06-01,          2024-07-01,          2024-08-01,
                2024-09-01,          2024-10-01,          2024-11-01,
                2024-12-01],
      dtype='object')

Now i want to filter values from those specific columns as per the below code:

df_Source[[col for col in df_Source.columns if col not in ('Country', 'Cost Component', 'Source', 'Internal/External') 
           and col >= datetime(2021,7,1)
           and col <= datetime(2022,6,1)]]

But this is giving me an error:

TypeError: '>=' not supported between instances of 'datetime.date' and 'str'

When the column names are datetime format, why does the above code not work?

Doesn't look like the columns are being converted to datetime on import.

Try doing this for your date columns after import:

df['Source'] = pd.to_datetime(df['Source']).apply(lambda x: x.date())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM