Python Converting Datetime into an integer

0 votes

I have an excel document with two columns, one of which is labelled "YYYY/MM/DD" and the other "HH:MM:SS." I then combined the two columns to make a "datetime" column that outputs the text "YYYY/MM/DD HH:MM:SS"

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.

Apr 6, 2023 in Others by Kithuzzz
• 38,000 points
1,198 views

1 answer to this question.

0 votes

Try this:

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
answered Apr 6, 2023 by narikkadan
• 63,600 points

Related Questions In Others

0 votes
0 answers

how to list the contents of a asset into an event

May 29, 2019 in Others by anonymous
676 views
0 votes
1 answer
0 votes
0 answers

How to convert a string to an integer in JavaScript?

How do I convert a string to ...READ MORE

May 2, 2022 in Others by Kichu
• 19,040 points
529 views
0 votes
1 answer

Excel VBA - Trouble importing WhatsApp chat history files into an Excel sheet

Since the OpenText method isn't working for ...READ MORE

answered Oct 14, 2022 in Others by narikkadan
• 63,600 points
739 views
0 votes
1 answer

Convert column in excel date format (DDDDD.tttt) to datetime using pandas

Given # s = df['date'] s 0 ...READ MORE

answered Oct 2, 2022 in Others by narikkadan
• 63,600 points
3,581 views
0 votes
1 answer

convert a SAS datetime in Pandas

SAS date value is a value that ...READ MORE

answered Jul 8, 2019 in Python by SDeb
• 13,300 points
3,330 views
0 votes
1 answer

Date difference: different results in Excel vs. Python

To calculate the date difference, we can ...READ MORE

answered Feb 23, 2022 in Database by gaurav
• 23,260 points
1,149 views
0 votes
1 answer

What are the RGB and HEX codes of the 3 color conditional format in Excel?

In Excel 2016 at least the colors ...READ MORE

answered Sep 24, 2022 in Others by narikkadan
• 63,600 points
8,755 views
0 votes
1 answer

How to paste an Excel chart into PowerPoint placeholder using Python?

Very near indeed! Copy and Paste are ...READ MORE

answered Oct 7, 2022 in Others by narikkadan
• 63,600 points
4,310 views
0 votes
1 answer

Embedding an excel document into an application with excel online

You have to do the heavy lifting ...READ MORE

answered Oct 2, 2022 in Others by narikkadan
• 63,600 points
915 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP