'Calculating calendar weeks from fiscal weeks
So I am really new to this and struggling with something, which I feel should be quite simple.
I have a Pandas Dataframe containing two columns: Fiscal Week (str) and Amount sold (int).
| Fiscal Week | Amount sold | |
|---|---|---|
| 0 | 2019031 | 24 |
| 1 | 2019041 | 47 |
| 2 | 2019221 | 34 |
| 3 | 2019231 | 46 |
| 4 | 2019241 | 35 |
My problem is the fiscal week column. It contains strings which describe the fiscal year and week . The fiscal year for this purpose starts on October 1st and ends on September 30th. So basically, 2019031 is the Monday (the 1 at the end) of the third week of October 2019. And 2019221 would be the 2nd week of March 2020. The issue is that I want to turn this data into timeseries later. But I can't do that with the data in string format - I need it to be in date time format.
I actually added the 1s at the end of all these strings using
df['Fiscal Week']= df['Fiscal Week'].map('{}1'.format)
so that I can then turn it into a proper date:
df['Fiscal Week'] = pd.to_datetime(df['Fiscal Week'], format="%Y%W%w")
as I couldn't figure out how to do it with just the weeks and no day defined. This, of course, returns the following:
| Fiscal Week | Amount sold | |
|---|---|---|
| 0 | 2019-01-21 | 24 |
| 1 | 2019-01-28 | 47 |
| 2 | 2019-06-03 | 34 |
| 3 | 2019-06-10 | 46 |
| 4 | 2019-06-17 | 35 |
As expected, this is clearly not what I need, as according to the definition of the fiscal year week 1 is not January at all but rather October.
Is there some simple solution to get the dates to what they are actually supposed to be?
Ideally I would like the final format to be e.g. 2019-03 for the first entry. So basically exactly like the string but in some kind of date format, that I can then work with later on. Alternatively, calendar weeks would also be fine.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
