ValueError in Python

Hi all,

I’m very amateur when it comes to Python coding, my job doesn’t necessarily require it. However, my predecessor was very good with it and left behind lots of code to improve many of the tasks that are done in my job.

Attached is a screenshot I am getting when running a line of code that I do on a monthly basis and I’ve never had issues with before. Can anyone decipher what exactly the error is and how I can go about fixing it?

Thanks in advance!

If it’s a problem with the code, I don’t think I can recommend a way to fix it without seeing the code base.
It looks like the Pandas library is what is throwing the error though, here’s the documentation for the Series.asof() function.

# This is the last line of code in "" to raise an error
# before the error stack moves into the Pandas library errors.
# Usually, this is the point where some error was introduced (or the error is with the Library...)
ds = sf_pn1_ad.index.asof(dsa).strftime('%Y-%m-%d %H:%M:%S')
  • sf_pn1_ad - Is likely a Pandas Time Series or DataFrame
  • sf_pn1_ad.index - Pulls the axis labels (index values) for the data (likely results in a list of dates without associated data-points)
  • sf_pn1_ad.index.asof(dsa) - Picks a particular date or set of dates from the axis labels (specifically it’s selecting the last date(s) before variable dsa) – Something Breaks Here
  • sf_pn1_ad.index.asof(dsa).strftime('%Y-%m-%d %H:%M:%S') - Converts the returned date object(s) to a String with the format “Year-Month-Day Hour:Minute:Second”

According to the documentation, the where argument of Series.asof() needs to be a:

date or array-like of dates

My guess is that the variable dsa (the where argument) contains an improper/non-parsable date. The remainder of the Pandas error stack seems to be problems with converting things to a Timestamp object, which is Pandas equivalent of a datetime object. I think that supports that theory. Without knowing where that dsa variable comes from though it’s anyone’s guess.

I am imagining this script runs through some kind of ledger and cleans/presents/analyzes/whatever? If dsa is constructed from that data, perhaps it picked up the error from the data and not the code itself. This is supported by the fact that you usually don’t have problems with this script. The code isn’t changing, the data it’s being run on probably is though.

Is it possible that an entry within that ledger has an invalid “date” attached to it?