3

I have a Pandas dataframe with many columns. The first column has dates listed as "Year-Month-Date" already set as a datetime type by using:

df_all['Date']=pd.to_datetime(df_all['Date'].astype(str),errors='coerce')

The data looks like:

0     2008-01-01 00:00:00   100   16250.0
1     2008-01-01 00:00:00   150   13740.0
2     2008-01-01 00:00:00   200   11900.0
3     2008-01-01 00:00:00   250   10460.0

I wish to simply drop the year so the column reads "Month-Date" without changing the other data in the columns associated with each row.

Jonathon
  • 251
  • 1
  • 7
  • 20
  • Possible duplicate of [How to change the datetime format in pandas](https://stackoverflow.com/questions/38067704/how-to-change-the-datetime-format-in-pandas) – Yuca May 02 '19 at 17:37

1 Answers1

0

If df_all['Date'] is string datatype, then you can use the str accessor with slicing like this:

df['Date'] = df_all['Date'].str[5:]

However, if df_all['Date'] is a datetime dtype then you can the date accessor with strfttime:

df_all['Date'] = df_all['Date'].dt.strftime('%m-%d %H:%M:%S')

Output:

             Date    A        B
0  01-01 00:00:00  100  16250.0
1  01-01 00:00:00  150  13740.0
2  01-01 00:00:00  200  11900.0
3  01-01 00:00:00  250  10460.0
Scott Boston
  • 147,308
  • 15
  • 139
  • 187