Home > Enterprise >  Can pandas format individual dates in a csv file?
Can pandas format individual dates in a csv file?

Time:08-08

I'm sure that this question has been asked before, but I can't seem to find a already existing question that has the same requirements as mine.

I have a CSV file with 3 columns, DATE, TOTAL and NAME.

           date   Total              name
0    2008-11-21      32     JOHN SMITH
1      08-25-21     7.9     JOHN SMITH
2      08-25-21     7.4     JOHN SMITH
3      08-27-21     100     JOHN SMITH
4    2008-04-21   36.62     JOHN SMITH

As you can see, there are 3 DATES that don't match the "yyyy-mm-dd" format. I want all the dates the follow the specified format. Is this something that is possible using Pandas?

CodePudding user response:

Usually, pd.to_datetime() is smart enough to infer the format on its own. To convert a series or a column of the dataframe to the datetime format you can use:

df["date"] = pd.to_datetime(df["date"]) 

You can then convert the series back to a string with the desired format:

df["date"].dt.strftime('%Y-%m-%d')

When working with (multiple) unusual formats you might need to use a different method, see this similar question.

CodePudding user response:

After importation and assuming they are imported as strings:

import numpy as np
df['date'] = np.where(len(df['date']) == 8, '20'   df['date'], df['date'])

CodePudding user response:

Following gtomer's suggestion and assuming these dates are wrongly formated and the format is the correct one you can also try with .str.pad:

s.str.pad(width=10, side='right', fillchar='20')

CodePudding user response:

Thank you for all the suggestions everyone, @psalts answer was the quickest solution for me:

df["date"] = pd.to_datetime(df["date"])

  • Related