Home > database >  While Working on google Collab for python, one of the csv file columns have dates in different forma
While Working on google Collab for python, one of the csv file columns have dates in different forma

Time:01-29

Dtype of column purchase_date is object. With values such as: 09-04-2012
8/21/2013

two different format. Trying to make them in single format.

Also looked for other methods where error is 'Series' object has no attribute year'

can some help to acheive the solution required.

attaching link for github where csv and my initial python code is written. https://github.com/prateek071995/Help.git

Option tried-

`def date_format(x): x=datetime.datetime.strptime(x,'%m/%d/%y') return x

project_data['new_']=project_data['purhcase_date'].apply(date_format) project_data ` Error- time data '09-04-2012' does not match format '%m/%d/%y'

CodePudding user response:

You can use pandas.to_datetime :

df = pd.read_csv("https://github.com/prateek071995/Help/blob/main/project_data.csv?raw=true")
​
df["purhcase_date"] = pd.to_datetime(df["purhcase_date"]) # <- if need a specific format, add `.dt.strftime("%d/%m/%Y")`

Output : ​

print(df)

     customer_id purhcase_date  ...  calls  intercoms
0       20201701    04/09/2012  ...      3         11
1       20201702    08/03/2014  ...      3         11
2       20201703    21/08/2013  ...      3         11
3       20201704    10/02/2014  ...      3         11
4       20201705    19/01/2014  ...      3         11
..           ...           ...  ...    ...        ...
494     20202195    27/11/2013  ...      3         11
495     20202196    13/04/2013  ...      3         11
496     20202197    05/04/2014  ...      3         11
497     20202198    21/04/2013  ...      6         11
498     20202199    22/05/2014  ...      6         11

[499 rows x 8 columns]
  • Related