Subtracting dates and only take out number of days as number - python

#Under d.types i can confirm they are both datetime objects
date1 datetime64[ns]
date2 datetime64[ns]
df_test['snaptoexpectedStart'] = df['date1'] - df['date2']
TypeError: '<' not supported between instances of 'str' and 'int'
I dont understand why I'm getting that error when both the columns im trying to subtract are in the correct format.

I guess it has something to do with the datetime format I suppose, try casting this way to see if it works :
from datetime import datetime
df_test['snaptoexpectedStart'] = datetime(df['date1']) - datetime(df['date2'])
If you are looking to get the number of days only than try this :
df_test['snaptoexpectedStart'] = (df_test['date1'] - df_test['date2Date']).dt.days

You might want to look into the timedelta class:
According to the API, subtracting two datetimes (assuming they are datetime.datetimes) results in a timedelta object. You can then use the .day attribute of the timedelta to get the difference in days.

Related

my code works for single input but whn i use lambda it doesnt work?

I change the field of 'date' to datetime in order to calculate the difference between their cells and 'today'
df['date']=pd.to_datetime(df['date'], format='%Y-%d-%m %H:%M:%S',errors='ignore')
so i can try below code for several random indexes such as below:
pd.Timestamp.now().normalize() - df['date'][1003]
Timedelta('2598 days 00:00:00')
as you see it works.
but when i run this code i got error:
df['Diff']=df['date'].agg(lambda x:pd.Timestamp.now().normalize()-x)
TypeError: unsupported operand type(s) for -: 'Timestamp' and 'str'
EDIT:
how can I recognize bad dates? i need all of dates with any NaT.
Filter missing values NaT generated if not match pattern %Y-%d-%m %H:%M:%S or if datetime not exist like 2020-02-30 12:02:10:
out = df.loc[pd.to_datetime(df['date'],format='%Y-%d-%m %H:%M:%S',errors='coerce').isna(), 'date']
Problem is with errors='ignore' it working different like you think.
It return same ouput if at least one wrong datetime, so is possible get mixed datetimes with strings or only string column (like original column date).
If need datetime column need replace bad dates to NaT by:
df['date']=pd.to_datetime(df['date'], format='%Y-%d-%m %H:%M:%S',errors='coerce')
df['Diff'] = pd.Timestamp.now().normalize() - df['date']

Get the average number of days between two dates in Python

I have a data frame with only dates in one column.
My objective is to calculate the number of days between each date, in order to get the average number of days between two dates (each date corresponds to an operation).
I tried doing something like this :
for i in range(len(df)):
if i != 0:
d0 = df.iloc[[i-1,1]]
d1 = df.iloc[[i,1]]
L.append((d1 - d0).days)
But got the error message : 'unsupported operand type(s) for -: 'str' and 'str''
You can subtract a date from another if they're in proper format. Maybe a timestamp in seconds, maybe a proper datetime object. As you might've noticed, you can't subtract strings.
If the date is in the ISO format, it's the easiest, just do this before the subtraction:
from datetime import datetime
d0 = datetime.fromisoformat(d0)
d1 = datetime.fromisoformat(d1)
The result will be in a datetime.timedelta format with a .total_seconds() method and even a .days attribute, so you get days like this:
difference_in_days = (d1 - d0).days
If it's something else than an ISO string, check out this question:
Converting string into datetime

Why does subtracting two UTC timestamps give "subtraction must have the same timezones or no timezones"?

I am trying to compare a Pandas datetime to a Python datetime.
The code
cur_t = pandas.Timestamp.utcnow()
Gives:
Timestamp('2019-02-14 14:31:40.283415+0000', tz='UTC')
And the code
data_t=indata.data.loc[2000].last_time
Gives:
Timestamp('2019-02-14 14:28:32+0000', tz='UTC')
However, subtracting data_t from cur_t gives TypeError: Timestamp subtraction must have the same timezones or no timezones
What is going on here? Why can't andas compare two times with the same timezone?

Date Comparison in python pandas

I have to compare two columns containing date values and find the difference between the 2 dates.
Out of the 2 columns one is of datetime type however another is an object type. W
hen trying to convert the object type to datetime using:
final['Valid to']=pd.to_datetime(final['Valid to'])
I am getting an error:
OutOfBoundsDatetime: Out of bounds nanosecond timestamp: 9999-12-31
00:00:00
How to convert the column of object type to datetime so that i can compare and get the required result?
use format parameter to provide the correct format of the string so that to_datetime can understand what type of string it is converting into datetime object
in your case it would be like
pd.to_datetime(s, format='%Y-%m-%d %H:%M:%S')
please post the sample data for the correct answer as someone have already written in the comment, that would be helpful.

How to compare dates in Python?

I need to see if a date has more than X days. How can I do this in Python?
I have tested something like:
if datetime.date(2010, 1, 12) > datetime.timedelta(3):
I got the error:
TypeError: can't compare datetime.date to datetime.timedelta
Any clue on how to achieve this?
You can't compare a datetime to a timedelta. A timedelta represents a duration, a datetime represents a specific point in time. The difference of two datetimes is a timedelta. Datetimes are comparable with each other, as are timedeltas.
You have 2 options:
Subtract another datetime from the one you've given, and compare the resulting timedelta with the timedelta you've also given.
Convert the timedelta to a datetime by adding or subtracting it to another datetime, and then compare the resulting datetime with the datetime you've given.
Comparing apples and oranges is always very hard! You are trying to compare "January 12, 2010" (a fixed point in time) with "3 hours" (a duration). There is no sense in this.
If what you are asking is "does my datetime fall after the nth day of the month" then you can do :
my_important_date = datetime.now()
if my_important_date.day > n:
pass #do you important things

Categories