0

I have an excel file with two columns, one is written as 'YYYY/MM/DD' and the other is written as 'HH:MM:SS'. I then combined the two columns to create a 'datetime' column that returns 'YYYY/MM/DD HH:MM:SS' in a string format.

df['datetime'] = df['YYYY/MM/DD'].astype(str) + ' ' + df['HH:MM:SS'].astype(str) 
df['datetime'] = pd.to_datetime(df['datetime'])

Now I want to convert datetime to an integer but am having no luck. This is what I have tried to do:

df['wtime'] = int(float(df['datetime']))

I just want an integer value returned so I can eventually get the start and end time of the data set.

FObersteiner
  • 22,500
  • 8
  • 42
  • 72
April F
  • 65
  • 4

2 Answers2

1

to convert your df['datetime'] you can do

df['newCol'] = df['datetime'].apply(lambda x: int(x.timestamp()))

this will create a column newCol containing the Unix timestamp as an integer

0

you can use this or another.

import datetime
current_date = datetime.datetime.now()
print("String Format of Current Date and Time:", current_date)
# String Format of Current Date and Time: 2022-11-16 13:19:43.211656

or

curr_date_str = str(current_date)
curr_date_strip = curr_date_str.split('.')[0]
curr_date_strip = curr_date_strip.replace(':', '')
curr_date_strip = curr_date_strip.replace('-', '')
curr_date_strip = curr_date_strip.replace(' ', '')
print("Stripped Format of Current Date and Time:", int(curr_date_strip))
# Stripped Format of Current Date and Time: 20221112200439