1

What I need to do is convert 'year-month-day' timestamp to Unix time stamp do somethings with it then change it back to date time series. I am working with '1999-09-07' as my timestamp. I am getting an error : invalid literal for int() with base 10: '1999-09-07'

df1['timestamp'] = df1['timestamp'].astype(np.int64) // 10**9
#Got back this 
ERROR:invalid literal for int() with base 10: '1999-09-07'


df1 = pd.read_csv('stock_CSV/' + ticker + '.csv')
pd.to_datetime(df1['timestamp'],unit='ns', origin='unix')
df1['timestamp'] = df1['timestamp'].astype(np.int64) // 10**9
#
#....some code
#
pd.to_datetime(df1['timestamp'], unit='s')

What I am expecting is a my dates converted to unix timestamp then converted back

David Long
  • 39
  • 6

1 Answers1

0

Calling astype('int64') on a Timestamp Series return the number of seconds even though the Timestamps may have a resolution of nanosecond.

You can do it the old way, by counting the seconds from Jan 1, 1970 to the timestamps:

# random epoch times, in nanoseconds
t = np.random.randint(1e9 * 1e9, 2e9 * 1e9, 10)

# convert to Timestamps
dates = pd.to_datetime(t, unit='ns')

# convert back to nanoseconds
epoch = (dates - pd.Timestamp('1970-01-01')).total_seconds() * 1e9

# verify that we did the conversion correctly
assert np.isclose(t, epoch).all()
Code Different
  • 90,614
  • 16
  • 144
  • 163
  • I am not sure with what you have here to put it together for my data. You have random times as t then converting them to date-time then subtracting Jan, 1 1970, maybe would I or how would I convert my timestamps from 'year-month-day' to seconds? – David Long Sep 16 '19 at 18:46
  • The Unix timestamp is simply the number of seconds since midnight on Jan 1, 1970. To get the nanoseconds, you multiply that by 1e9 – Code Different Sep 16 '19 at 18:47