I am trying to extract the epiweek from a pandas dataframe with the library "epiweeks" but i have some problems doing it.
mi code is below
import pandas as pd
from epiweeks import Week, Year
a= [('2021-06-11 00:00:00'),
('2021-06-11 00:00:00'),
('2021-06-11 00:00:00'),
('2021-06-11 00:00:00'),
('2021-06-11 00:00:00'),
('2021-06-11 00:00:00'),
('2021-06-18 00:00:00'),
('2021-06-18 00:00:00'),
('2021-06-18 00:00:00'),
('2021-06-18 00:00:00'),
('2021-06-18 00:00:00'),
('2021-06-18 00:00:00'),
('2021-06-18 00:00:00')]
b = [25,58,65,45,47,85,96,36,25,14,25,78,75]
fecha = pd.DataFrame(list(zip(a,b)))
fecha[0] = pd.to_datetime(fecha[0])
Week.fromdate(fecha[0])
with this code I get: AttributeError: 'Series' object has no attribute 'year'
can you help me to find my wrong
Do you know another way to do it?
Thank you!
CodePudding user response:
Easy!
The only thing that you're doing wrong is you pass the whole Series
to fromdate
:
Week.fromdate(fecha[0])
That won't work, because Week.fromdate
expects a datetime
or a pandas.Timestamp
. But you can call fecha[0].apply
on the series, which takes a function, and calls that function for each item in the series, and adds the value that the function returns to a new series, and then returns that series. You can then add that series to the dataframe like so:
fecha['week'] = fecha[0].apply(Week.fromdate)
Or, if you want to pass some custom parameters to Week.fromdate
:
fecha['week'] = fecha[0].apply(lambda x: Week.fromdate(x, ...))
Try it:
>>> fecha
0 1 week
0 2021-06-11 25 202123
1 2021-06-11 58 202123
2 2021-06-11 65 202123
3 2021-06-11 45 202123
4 2021-06-11 47 202123
5 2021-06-11 85 202123
6 2021-06-18 96 202124
7 2021-06-18 36 202124
8 2021-06-18 25 202124
9 2021-06-18 14 202124
10 2021-06-18 25 202124
11 2021-06-18 78 202124
12 2021-06-18 75 202124
>>> fecha['week'][0]
Week(2021, 23, CDC)
>>> fecha['week'][12]
Week(2021, 24, CDC)