A date with a difficult customer

Being an SAP consultant, I’m working with SAP data in Pandas quite a lot. It’s common practice to set a “forever” date of 31.12.9999 in SAP and the system handles it, however Pandas has a limitation in this regard where the date cannot readily go beyond the year 2262 which caused me a few headaches this morning!

Fortunately, for my purposes I can work with any viable “forever” type date, so thanks to jezrael via Stackoverflow I now have a workable solution:



# Use .to_datetime() method to convert dates in combinations with errors = "coerce" which forces NaT 
# where "non-dates" like 31.12.9999 occur
users["Valid to"] = pd.to_datetime(users["Valid to"], errors = "coerce") 
# Then use .fillna() method to replace NaT with another viable "forever" type date
# pd.Timestamp() is used to tell Pandas "I'm giving you a date here"
users["Valid to"].fillna(pd.Timestamp("2200-12-31"), inplace = True)


Comments are closed.

Create a website or blog at WordPress.com

Up ↑

%d bloggers like this: