Python pandas dataframe conversion to unixtime [duplicate] - python

This question already has answers here:
pandas datetime to unixtime
(2 answers)
Closed 2 years ago.
I would like to convert a date of the format with yyyy=year, mm=month, dd=day, hh=hour, nn=minute in a unix timestamp.
I tried:
df_out['unixtime'] = datetime(df_out['yyyymmddhhmm'].dt.year.to_numpy(),df_out['yyyymmddhhmm'].dt.month.to_numpy(),df_out['yyyymmddhhmm'].dt.day.to_numpy(),df_out['yyyymmddhhmm'].dt.hour.to_numpy(),df_out['yyyymmddhhmm'].dt.minute.to_numpy()).timestamp()
but I got the error message:
TypeError: only size-1 arrays can be converted to Python scalars
What am I doing wrong?
Any help is highly appreciated!
Regards,
Alexander

The officially recommended way is to subtract the epoch and then to floor-divide by the “unit” (1 second):
df = pd.DataFrame({'yyyymmddhhmm': pd.to_datetime(['20201108121314', '20201109121314'])})
df['unixtime'] = (df.yyyymmddhhmm - pd.Timestamp('1970-01-01')) // pd.Timedelta('1s')
Result:
yyyymmddhhmm unixtime
0 2020-11-08 12:13:14 1604837594
1 2020-11-09 12:13:14 1604923994

You can create a single column for the date using the pandas library
df_out['date_format'] = pd.to_datetime(df_out['date_time_column'], format='%Y%m%d%H%M')
Then you can create new columns which will consist of year, month, date, hour info by
pd.DatetimeIndex(df_out['date_format']).year
pd.DatetimeIndex(df_out['date_format']).month
pd.DatetimeIndex(df_out['date_format']).day
pd.DatetimeIndex(df_out['date_format']).hour

Related

pandas calculate time difference to now [duplicate]

This question already has answers here:
Compare timestamp with datetime
(2 answers)
Timestamp String in Zulu Format To Datetime
(1 answer)
Closed 9 months ago.
I have a pandas dataframe df with a time column containing datetime values. I now want to filter the dataframe to show rows with time values lying in the next 15 minutes.
So first I try to simply subtract the current time from the datetimes.
df.Time = pd.to_datetime(df.Time)
print(df.Time - pd.to_datetime("today"))
But got this error:
TypeError: Cannot subtract tz-naive and tz-aware datetime-like objects
I tried to remove the tz-awareness with .replace(tzinfo=None) but it was not working. In the end I am looking for a command like this (assuming the difference of two datetimes in is minutes):
df.loc[df.Time - pd.to_datetime("today") < 15]

Converting datetime to date (Difference in Integer or Number format) [duplicate]

This question already has answers here:
Python: Convert timedelta to int in a dataframe
(6 answers)
Closed 1 year ago.
I have multiple columns with datetime format "14-09-2021 12:00:00 AM". I have converted them to date with the below code.
df['Column1'] = pd.to_datetime(df['Column1']).dt.date
df['Column2'] = pd.to_datetime(df['Column2']).dt.date
Intention is to take the difference between the two columns to identify the difference in days. When I take the difference df['diff']=df['Column1']-df['Column2'] I get the result as say "- 39 days" and not jus "- 39" (Image added below for clarity)
Intention is to get the difference in integer or number format (i.e just 39 and not 39 days), I'm not sure on how to go about this or when I have erred in the above code.
Help would be much appreciated.
Try this df['diff'] = df['diff'].astype(int)

Python - convert number of hours to a date? [duplicate]

This question already has answers here:
Converting unix timestamp string to readable date
(19 answers)
Closed 2 years ago.
I have in Python 3.7 a date in form of an integer that represents the number of hours from 1/1/1900 00 Hours. Can I transform it into string of format dd/mm/yyyy?
for example:
timenumber = 1043148
timestring = magictrickfunctions(timenumber)
print(timestring)
should give "01/01/2017"
Are you sure it gives you 01/01/2017 instead of 01/01/2019?
To obtain the number of days divide your total hours by /24 this will give you the number of days. You can then specify that as shown below. I am using pandas library here.
import pandas as pd
start_date = "01/01/1900"
date_1 = pd.to_datetime(start_date)
end_date = date_1 + pd.DateOffset(days=43464) #specify the number of days and add it to your start_date
print(end_date)
Outtput
2019-01-01 00:00:00

how to convert object column in to datetime? [duplicate]

This question already has answers here:
ValueError: day is out of range for month
(2 answers)
Closed 3 years ago.
I am trying to convert a column type to datetime
Value Format in Column: '2016-04-10 12:17:52'
df['dropoff_time']
output
0 2016-04-10 12:17:52
1 2016-04-13 06:44:12
2 2016-04-13 06:54:43
3 2016-04-13 08:33:50
Name: created_at_new, Length: 328, dtype: object
I am trying the following code:
df['created_at_new'] = pd.to_datetime(df['created_at_new'])
ValueError: day is out of range for month
Desired result is a datetime
('2010-11-12 00:00:00')
When I tried with the same example, it worked for me. Anyways in order to rectify the error, you can try the following:
Check whether you have the latest version of pandas. If not Update it and
Try mentioning the date format
df['created_at_new'] = pd.to_datetime(df['created_at_new'], format='%Y-%m-%d %H:%M:%S')
Still, if it doesn't work. You can skip the one with error using the argument errors='coerce'. In the place of the skipped one, 'NaT' value will be added.
For more details, you check out this answer.

How to use Pandas calculate # of weeks/days? [duplicate]

This question already has answers here:
Pandas Datetime: Calculate Number of Weeks Between Dates in Two Columns
(2 answers)
Closed 4 years ago.
Hi, I have a dataframe with date columns. I want to add a column to calculate how many weeks since the contact? For example, today's date is 20-Sep-18, and use this date to calculate with the column.
Can anyone help me with this questions? Thanks!
You can do like this.
df['Contact Date']= pd.to_datetime(df['Contact Date'])
import datetime
df['How Many days'] = datetime.datetime.now() - df['Contact Date']

Categories