stratusshow
stratusshow

Reputation: 25

How do I convert from int64 back to timestamp or datetime'?

I am working on a project to look at how much a pitcher's different pitches break each game. I looked here for an earlier error which fixed my error but it gives me some weird numbers. What I mean is like when I print what I hope to be August 3rd,2020 I get 1.5964128e+18. Here's how I got there.

hughes2020=pd.read_csv(r"C:/Users/Stratus/Downloads/Hughes2020Test.csv",parse_dates=['game_date'])
game=hughes2020['game_date'].astype(np.int64)
#Skipping to next part to an example
elif name[i]=="Curveball":
   if (c<curve)
      xcurve[c]=totalx[i]
      ycurve[c]=totaly[i]
      cudate[c]=game[i]
      c+=1

and when I print the cudate it gives me the large number and I am wondering how I can change it back.

And if I run it as

game=hughes2020['game_date'] #.astype(np.int64)
    #Skipping to next part to an example
    elif name[i]=="Curveball":
       if (c<curve)
          xcurve[c]=totalx[i]
          ycurve[c]=totaly[i]
          cudate[c]=game[i]
          c+=1

It gives me an

TypeError: float() argument must be a string or a number, not 'Timestamp'

Upvotes: 0

Views: 1256

Answers (1)

Poe Dator
Poe Dator

Reputation: 4903

To convert int to datetime use pd.to_datetime():

df = pd.DataFrame(data=[1.5964128e+18], columns = ['t'])
df['t2'] = pd.to_datetime(df['t'])

              t         t2
0  1.596413e+18 2020-08-03

However a better solution would be to convert the dates at the time of csv reading (As @sinanspd correctly pointed out). Use parse_dates and other related options in pd.read_csv(). Function manual is here

Upvotes: 1

Related Questions