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]
Related
This question already has answers here:
How to change the datetime format in Pandas
(8 answers)
How can I format date time string which is not date time format so that I can use it with pd.to_datetime()?
(2 answers)
Convert DataFrame column type from string to datetime
(6 answers)
Closed 5 months ago.
I have a 'date' column that has a date value as 20170423 (yyyymmdd) how can i change it to 2017-04-23?
dataframe = df
column name 'date'
I read the below post, but none of the solutions worked
Fastest way to insert these dashes in python string?
Example
d = df['Date']
df['Date'] = '%s-%s-%s' % (d[:4], d[4:6], d[6:])
The output 0 0 20170711\n1 20170718\n2 20170718\n3... when i exported in .csv
This question already has answers here:
Pandas: extract hour from timedelta
(4 answers)
Pandas: Subtracting two date columns and the result being an integer
(5 answers)
Subtracting Dates to get Days in pandas
(1 answer)
Closed 2 years ago.
I have a dataset like this
data = pd.DataFrame({'order_date-time':['2017-09-13 08:59:02', '2017-06-28 11:52:20', '2018-05-18 10:25:53', '2017-08-01 18:38:42', '2017-08-10 21:48:40','2017-07-27 15:11:51',
'2018-03-18 21:00:44','2017-08-05 16:59:05', '2017-08-05 16:59:05','2017-06-05 12:22:19'],
'delivery_date_time':['2017-09-20 23:43:48', '2017-07-13 20:39:29','2018-06-04 18:34:26','2017-08-09 21:26:33','2017-08-24 20:04:21','2017-08-31 20:19:52',
'2018-03-28 21:57:44','2017-08-14 18:13:03','2017-08-14 18:13:03','2017-06-26 13:52:03']})
I need to calculate the delivery delay for this data
I did this to change it to a dattime data
data['order_date-time']=pd.to_datetime(data['order_date-time'])
data['delivery_date_time']=pd.to_datetime(data['delivery_date_time'])
then I calculated the
data['delivery delay']= data['delivery_date_time']-data['order_date-time']
and new column looks like this in the output
7 days 14:44:46
15 days 08:47:09
...
how can I change these column values to int values like 7, 15, .. without "days" and time?
Subtracting two datetime columns from each other gives you a column of dtype timedelta. You can call the days attribute of a timedelta column with the dt accessor:
data['delivery delay'].dt.days
0 7
1 15
2 17
3 8
...
...or if you need fractional days, call the total_seconds and divide by the seconds in a day:
data['delivery delay'].dt.total_seconds()/86400
0 7.614421
1 15.366076
2 17.339271
3 8.116563
...
Unfortunately, you can't format timedelta to string as you can with datetime, see also Format timedelta to string.
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.
This question already has answers here:
How to compare two dates?
(6 answers)
Closed 3 years ago.
i want to compare/subtract two date's due date from current date in python and apply a condition on this subtraction that if the difference is >0 days then calculate fine by multiplying difference with fine per day
from datetime import date
a=date.today()
issuedate=date(2019,5,9)
duedate#should be 5 days after issue date i can't find the method for doing this
check=a-duedate
# if check>0days:
# print(check days*40)
You need to use the timedelta function in the datetime module:
from datetime import date
import datetime
a=date.today()
issuedate=date(2019,5,9)
duedate = issuedate+datetime.timedelta(days=5)
check=a-duedate
print(check>=datetime.timedelta(days=0))
Use timedelta
from datetime import timedelta
duedate = issuedate + timedelta(days=5)
check=(a-duedate).days
This question already has answers here:
How to convert a datetime format to minutes - pandas
(2 answers)
Closed 4 years ago.
diff=table['DelayInMinute']=(pd.to_datetime(table['joinTime']) -
pd.to_datetime(table['ScheduleDateTime']))
Output: "0 days 00:00:00"
diff_minute=pd.Timedelta(diff).total_seconds()/60.0;
But getting
ValueError: Value must be Timedelta, string, integer, float, timedelta
or convertible
You can use Series.dt.total_seconds if multiple values of Series:
diff_minute=diff.dt.total_seconds()/60.0
If want output scalar by first value use iat for select first value with Timedelta.total_seconds, converting to Timedelta is not necessary:
diff_minute=diff.iat[0].total_seconds()/60.0