I have an array of date strings, eg: ["1999-2-4", "1989-2", "2020", "1914/09/01"]
I'm converting these strings to timestamps with pandas' to_datetime.
But I get back a standard pandas datetime with ns precision. I need some way of also knowing what the original precision of the string was (ie [day, month, year, day] for the array above)
What I initially tried was setting up an array of formats matched with an array of precisions:
1: ["%Y-%M-%D", "%Y/%M/%D", "%Y-%M", "%Y"]
2: ["day", "day", "year", "month"]
and I planned on simply trying each format in order until one worked, and then taking the matching precision.
However, unfortunately (for my purposes), an input like "1999" passed to to_datetime with format="%Y-%M-%D", even with exact=True, will successfully parse. So there went the plan of relying on try-catching in a loop.
I need some way of getting the original precision. Is this possible with pandas? Alternatively, is this possible with dateutils?