calculate date difference between today's date and pandas date series

Viewed 13017

Want to calculate the difference of days between pandas date series -

0      2013-02-16
1      2013-01-29
2      2013-02-21
3      2013-02-22
4      2013-03-01
5      2013-03-14
6      2013-03-18
7      2013-03-21

and today's date.

I tried but could not come up with logical solution. Please help me with the code. Actually I am new to python and there are lot of syntactical errors happening while applying any function.

4 Answers

You could do something like

# generate time data
data = pd.to_datetime(pd.Series(["2018-09-1", "2019-01-25", "2018-10-10"]))
pd.to_datetime("now") > data

returns:

0    False
1     True
2    False

you could then use that to select the data

data[pd.to_datetime("now") > data]

Hope it helps.

Edit: I misread it but you can easily alter this example to calculate the difference:

data -  pd.to_datetime("now")

returns:

0   -122 days +13:10:37.489823
1      24 days 13:10:37.489823
2    -83 days +13:10:37.489823
dtype: timedelta64[ns]

You can try as Follows:

>>> from datetime import datetime
>>> df
        col1
0 2013-02-16
1 2013-01-29
2 2013-02-21
3 2013-02-22
4 2013-03-01
5 2013-03-14
6 2013-03-18
7 2013-03-21

Make Sure to convert the column names to_datetime:

>>> df['col1'] = pd.to_datetime(df['col1'], infer_datetime_format=True)

set the current datetime in order to Further get the diffrence:

>>> curr_time = pd.to_datetime("now")

Now get the Difference as follows:

>>> df['col1'] - curr_time
0   -2145 days +07:48:48.736939
1   -2163 days +07:48:48.736939
2   -2140 days +07:48:48.736939
3   -2139 days +07:48:48.736939
4   -2132 days +07:48:48.736939
5   -2119 days +07:48:48.736939
6   -2115 days +07:48:48.736939
7   -2112 days +07:48:48.736939
Name: col1, dtype: timedelta64[ns]

With numpy you can solve it like difference-two-dates-days-weeks-months-years-pandas-python-2 . bottom line

df['diff_days'] = df['First dates column'] - df['Second Date column']

# for days use 'D' for weeks use 'W', for month use 'M' and for years use 'Y'
df['diff_days']=df['diff_days']/np.timedelta64(1,'D')      
print(df) 

if you want days as int and not as float use

df['diff_days']=df['diff_days']//np.timedelta64(1,'D')      

From the pandas docs under Converting To Timestamps you will find:

"Converting to Timestamps To convert a Series or list-like object of date-like objects e.g. strings, epochs, or a mixture, you can use the to_datetime function"

I haven't used pandas before but this suggests your pandas date series (a list-like object) is iterable and each element of this series is an instance of a class which has a to_datetime function.

Assuming my assumptions are correct, the following function would take such a list and return a list of timedeltas' (a datetime object representing the difference between two date time objects).

from datetime import datetime

def convert(pandas_series):
    # get the current date
    now = datetime.now()

    # Use a list comprehension and the pandas to_datetime method to calculate timedeltas.
    return [now - pandas_element.to_datetime() for pandas_series]

# assuming 'some_pandas_series' is a list-like pandas series object
list_of_timedeltas = convert(some_pandas_series)
Related