2

I recently asked a question about identifing all the columns which are datetime. Here it is: Get all columns with datetime type using pandas?

The answer was correct for a proper date time format, however, I now realize my data isn't proper date time, it is a string formatted like "2017-02-12 10:23:55 AM" and I was advised to create a new question.

I have a huge dataframe with an unknown number of date time columns, where I do not know their names nor their position. How do I identify the column names of the date time columns which have the date of format such as YYYY-MM-dd hh:mm:ss AM/PM?

Community
  • 1
  • 1
J. Doe
  • 347
  • 1
  • 2
  • 6

1 Answers1

1

One way to do this would be to test for successful conversion:

def is_datetime(datetime_string):
    try:
        pd.to_datetime(datetime_string)
        return True
    except ValueError:
        return False

With this:

dt_columns = [c for c in df.columns if is_datetime(df[c][0])]

Note: This tests for any string that can be converted to a datetime.

Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
  • I see, thank you. I was trying to avoid for loops incase there was a build-in function. But thanks! – J. Doe Feb 19 '17 at 04:19