I am working with Python in Spotfire and am trying to convert fiscal weeks to the date of the Monday of the input fiscal week.
I have attempted to implement the solution provided here to no avail. My script is as follows:
import datetime
d= datetime.datetime.strptime(str(fw), "%Y-%W-%w")
Input values look like the values below:
Input |
---|
2023-06-1 |
2023-08-1 |
2023-13-1 |
2023-12-1 |
The full text of the error is below:
Could not execute function call 'date_from_fw'
Error executing Python script:
ValueError: time data '0 2021-17-1\n1 2023-11-1\n2 2023-12-1\n3 2021-24-1\n4 2022-39-1\n ... \n82248 2024-09-1\n82249 2024-10-1\n82250 2022-45-1\n82251 2022-33-1\n82252 2022-33-1\nName: fiscal_week_str, Length: 82253, dtype: object' does not match format '%Y-%W-%w'
Traceback (most recent call last):
File "data_function.py", line 333, in _execute_script
exec(compiled_script, self.globals)
File "<data_function>", line 2, in <module>
File "_strptime.py", line 577, in _strptime_datetime
tt, fraction, gmtoff_fraction = _strptime(data_string, format)
File "_strptime.py", line 359, in _strptime
(data_string, format))
at Spotfire.Dxp.Data.DataFunctions.Executors.LocalPythonFunctionClient.<RunFunction>d__8.MoveNext()
at Spotfire.Dxp.Data.DataFunctions.Executors.PythonScriptExecutor.<ExecuteFunction>d__11.MoveNext()
at Spotfire.Dxp.Data.DataFunctions.DataFunctionExecutorService.<ExecuteFunction>d__8.MoveNext()
Found a solution without using the strptime function. This is assuming a column series input with fiscal week values formatted as 'YYYY-WW'.
import datetime
import pandas as pd
# fw is input in format 'YYYY-WW'
# Add weekday number to string 1 = Monday
fw = fw + '-1'
# dt is output column
# Use %G-%V-%w if input is in ISO format
dt = pd.to_datetime(fw, format='%Y-%W-%w', errors='coerce')