This question already has answers here:
Pandas Timedelta in Days
(5 answers)
Closed 3 years ago.
So, I have a pandas dataframe with a lot of variables including start/end date of loans.
I subtract these two in order to get their difference in days.
The result I get is of the type i.e. 349 days 00:00:00.
How can I keep only for example the number 349 from this column?
Check this format,
df['date'] = pd.to_timedelta(df['date'], errors='coerce').days
also, check .normalize() function in pandas.
Related
This question already has answers here:
Convert Pandas Column to DateTime
(8 answers)
How to change the datetime format in Pandas
(8 answers)
Closed last month.
I want to convert the date column in my dataframe which is in the format of Dec. 01, 2022 to 12-01-2022
I tried using map function and by importing calender as well
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]
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)
This question already has answers here:
Add a sequential counter column on groups to a pandas dataframe
(4 answers)
Closed 4 years ago.
if I have a data set of time series and I want to estimate the number of the day of a groupby time series per each day as seen in the figure and act as a counter :
nothing special in my code yet, it is just reading the data and convert time and day into
import pandas as pd
df = pd.read_csv('*file location and name*',sep=",")
df.head()
df['Date'] =pd.to_datetime(df['Date']+" "+df['Time'])
df.set_index('Date', inplace=True)
See if answers your query:
df['dayOfMonth']= df.groupby('day').cumcount() + 1
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']