0

I was wondering if someone can take a look at my formatting code and can explain to me why some of my data looks like 2009/09/11 and 2009-09-11 intermittently. Like one row its 2009/09/11 and another its formatted as 2009-08-11. I am trying to set it up for a ds field for neuroProphet but for some reason it isn't wanting to format it to a datetime object. The original dates look like this 20090911.

df['year'] = df['ds'].apply(lambda x:str(x)[:-4])
df['month'] = df['ds'].apply(lambda x:str(x)[-6:-4])
df['day'] = df['ds'].apply(lambda x: str(x)[-2:])
df['ds'] = df['year'] + '/' + df['month']+ '/' + df['day']
df['ds'] = pd.to_datetime(df['ds'],format='%Y/%m/%d') 



ValueError: time data '2009/09/18' does not match format '%y/%m/%d' (match)

I tried to re-run the cells but they keep on formatting to either 2009/09/11 or 2009/09/11 when I export it to a csv(to look at it more closely because I am having a mismatch error). I am wondering if excel automatically formats it but I am not sure. As an added note I do switch

df['ds'] = df['year'] + '/' + df['month']+ '/' + df['day']

to

df['ds'] = df['year'] + '-' + df['month']+ '-' + df['day']

to see if it fixes it. I am wondering if its google collabs fault but I am not sure what is entirely going on.

FObersteiner
  • 22,500
  • 8
  • 42
  • 72
  • 1
    Your example shows `format='%Y/%m/%d')` but the error message shows `'%y/%m/%d'` - uppercase y in one and lowercase in the other. Did you forget to save an edit? – wwii Feb 02 '23 at 23:28
  • Lower-case `y` is for parsing 2-digit year (upper-case for 4-digit). – FObersteiner Feb 03 '23 at 04:29
  • possibly. I have been playing with Uppercase Y. I swear if it works because of that I am throwing a table. – Travis Hulsey Feb 03 '23 at 16:37

0 Answers0