2

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()
igeis
  • 31
  • 5
  • 2
    It appears that `fw` is an array. I suppose you meant to pass only one element of that array to `strptime`. Now what exactly is your question about this? – mkrieger1 Jan 11 '23 at 22:29
  • Not enough information in your question. The data being complained about looks like a string representation of a Pandas Series and I see stuff in the Spotfire docs saying a Spotfire column *maps to* a Pandas Series. We really don't know what you intended. Please read [mre]. – wwii Jan 11 '23 at 23:08
  • [https://pandas.pydata.org/docs/reference/api/pandas.to_datetime.html](https://pandas.pydata.org/docs/reference/api/pandas.to_datetime.html) – wwii Jan 11 '23 at 23:13
  • @mkrieger1, I do believe you are right that Spotfire is passing all of the column values in as an array. From Python's `strptime` documentation, it seems the function only accepts single string values. Does this mean I should loop through the input array inserting each value into `strptime`, and append each result to an output array `d`? – igeis Jan 12 '23 at 16:23

1 Answers1

0

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')
igeis
  • 31
  • 5