TypeError: descriptor 'strftime' requires a 'datetime.date' object but received a 'str'

Hi! I am having a hard time changing my date column to datetime. Help, please!

for row in bike_sharing:
date_cycled = row[0]
date_cycled = dt.date.strftime(date_cycled,’%d/%m/%Y’)
row[0] = date_cycled

for row in bike_sharing:
date_cycled = row[0]
date_cycled = dt.date.strftime(date_cycled,’%d/%m/%Y’)
row[0] = date_cycled

print(date_cycled)

TypeError Traceback (most recent call last)
in
1 for row in bike_sharing:
2 date_cycled = row[0]
----> 3 date_cycled = dt.date.strftime(date_cycled,’%d/%m/%Y’)
4 row[0] = date_cycled
5

TypeError: descriptor ‘strftime’ requires a ‘datetime.date’ object but received a ‘str’

Hi @naceval1
I think you should first use datetime.strptime() constructor to returns datetime object, Can you add link to the mission.

1 Like

Hi bahmed21,
I am working on a personal project as I continue with the studies but I based myself with the mission 5. Using Strptime to Parse Strings as Dates. I already imported the datetime as dt.

You can check out a rough draft of the project -> https://github.com/naceval1/6_months_cycling_data_project

Hi @naceval1,
To creates a datetime object I will do something like this

dt_object = dt.datetime.strptime(speedometer['elapsed_time'],"%H:%M:%S")

but this is correct if you have string as input not series

TypeError: must be str, not Series

thanks for the feedback. Wouldn’t I need to run a for loop to change the whole column?

I used your code but I still get TypeError: strptime() argument 1 must be str, not Series
I tried using pandas.Series.to_string but it doesnt work

Hi @naceval1,

you can use pd.to_datetime to convert your date column to datetime

date_series = pd.to_datetime(date_string)

I find this answer in stackoverflow
https://stackoverflow.com/questions/38797854/applying-strptime-function-to-pandas-series

1 Like

Hi, thanks for your help but no luck. I am out of clues of how to fix this. The other option I guess is to directly change the spreadsheet?

Hi Bahmed, I was able to convert speedometer[‘date’] = pd.to_datetime(speedometer[‘date’], format="%m/%d/%y") see below. Its weird that I have 3 more columns to change but it creates an error.

date elapsed_time trip_distance average_speed maximum_speed
0 2019-06-08 1:50:13 34.57 18.8 38.8
1 2019-06-09 1:07:39 17.23 15.3 32.7
2 2019-06-10 :47:47 16.45 20.7 36.8
4 2019-06-12 1:45:41 35.15 19.9 39.5
5 2019-06-13

Hi Bahmed!

I finally was able to change datetime, thanks a lot. but now my times have dates :confused:

This is the input:
bike_sharing[‘date’] = pd.to_datetime(bike_sharing[‘date’], format= “%d/%m/%Y”)
bike_sharing[‘elapsed_time’] = pd.to_datetime(bike_sharing[‘elapsed_time’], format="%M:%S")

This is the output
date elapsed_time
0 2019-12-28 1900-01-01 00:07:11
1 2019-12-28 1900-01-01 00:03:22
2 2019-12-28 1900-01-01 00:03:24
3 2019-12-28 1900-01-01 00:19:20
4 2019-12-28 1900-01-01 00:09:56
… … …
137 2019-07-07 1900-01-01 00:10:43
138 2019-07-07 1900-01-01 00:09:20
139 2019-07-06 1900-01-01 00:21:42
140 2019-07-06 1900-01-01 00:06:02
141 2019-07-06 1900-01-01 00:04:45

[142 rows x 2 columns]

Hi @naceval1,
I find this example
https://www.geeksforgeeks.org/python-pandas-to_datetime/
and it seems like they are also having time with dates
you can try this code

bike_sharing['elapsed_time'] = pd.to_datetime(bike_sharing['elapsed_time'], format="%H:%M:%S").dt.time