Jan-14-2021, 03:45 PM
Hello,
I am working with covid-19 data, and I am trying to add a new column to the dataframe based on the datetime, converting it to a day of the week.
So I tried this:
I am working with covid-19 data, and I am trying to add a new column to the dataframe based on the datetime, converting it to a day of the week.
import pandas as pd url='https://raw.githubusercontent.com/nytimes/covid-19-data/master/us-states.csv' data = pd.read_csv(url) data.head()The output of that looks like this:
Output:date state fips cases deaths
0 2020-01-21 Washington 53 1 0
1 2020-01-22 Washington 53 1 0
2 2020-01-23 Washington 53 1 0
3 2020-01-24 Illinois 17 1 0
4 2020-01-24 Washington 53 1 0
What I want to do, is to add a column called 'dow' that converts the date into the actual day of week.So I tried this:
data['DOW'] = datetime.date(data['date']).strftime('%A')But that is giving me this error:
Error:TypeError Traceback (most recent call last)
<ipython-input-33-ae85c40abf8d> in <module>()
1 data['DOW'] = 'Day'
----> 2 print(datetime.date(data['date']).strftime('%A'))
/usr/local/lib/python3.6/dist-packages/pandas/core/series.py in wrapper(self)
127 if len(self) == 1:
128 return converter(self.iloc[0])
--> 129 raise TypeError(f"cannot convert the series to {converter}")
130
131 wrapper.__name__ = f"__{converter.__name__}__"
TypeError: cannot convert the series to <class 'int'>
I would appreciate any help!