# How to get date columns in pandas?

   id   id2     post    id  level   created_at
0   150     126293  BA     237602   0   2018-10-08 15:37:06.404741
1   150     126293  BA     217350   0   2018-09-26 11:27:20.610651
2   4655    169068  BA     172174   1   2018-08-07 15:54:15.961607
3   14058   201462  BA      231703  1   2018-10-04 18:26:06.915134
4   16576   241053  agent   416451  2   2019-02-06 16:59:41.550580
5   16576   241053  agent   423247  2   2019-02-10 18:10:12.987203
6   16700   102938  BA     476606   0   2019-03-07 23:23:36.902590
7   50374   131808  TSM     203392  0   2018-09-13 21:54:45.874108
8   50374   131808  TSM     103873  0   2018-03-23 17:30:20.647447

• You may read documentation before asking. date handling, column selection Apr 2, 2019 at 9:32
• Did you mean to get only the date (2018-10-08) and remove the time? Apr 2, 2019 at 11:02

df['date'] = pd.to_datetime(df['created_at']).dt.date


to_datetime: Convert argument to datetime. For example, if your column of "created_at" is a string column, it converts it to a datetime column

dt: Access object for datetime like properties of the Series values.

• This is probably the best (code) answer so far, but you should probably include some words e.g explaining why first using pd.to_datetime is necessary and what .dt. is. Links to documentation are also a good idea :) May 30, 2020 at 7:54
• Now it is a complete answer! :) May 30, 2020 at 17:42

You can achieve this by using the following code:

date = df['created_at']
df.drop('created_at', inlace = True)

• Please consider adding some explanation of what your code does. Ultimately, the output of it. Apr 2, 2019 at 9:34
  df['date'] = df['created_at'].str.extract('(....-..-..)', expand=True)


If the created_at column is of the datetime type, you can use the .dt method to only get the date as follows:

df["date_column"] = df["created_at"].dt.date


This will return the following column:

date_column
2018-10-08
2018-09-26
2018-08-07
2018-10-04
2019-02-06
2019-02-10
2019-03-07
2018-09-13
2018-03-23