Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas: number of days elapsed since a certain date

I have a dataframe with a 'date' column with ~200 elements in the format yyyy-mm-dd.

I want to compute the number of days elapsed since 2001-11-25 for each of those elements and add a column of those numbers of elapsed days to the dataframe.

I know of the to_datetime() function but can't figure out how to make this happen.

like image 283
jeremy radcliff Avatar asked Sep 27 '14 06:09

jeremy radcliff


People also ask

How do you count the number of occurrences in pandas?

Using the size() or count() method with pandas. DataFrame. groupby() will generate the count of a number of occurrences of data present in a particular column of the dataframe.

What does .values in pandas do?

Definition and Usage The values property returns all values in the DataFrame. The return value is a 2-dimensional array with one array for each row.

How do I get date and time values in pandas?

Let’s get started. The pandas library provides a DateTime object with nanosecond precision called Timestamp to work with date and time values. The Timestamp object derives from the NumPy’s datetime64 data type, making it more accurate and significantly faster than Python’s DateTime object.

What is the difference between pandas timestamp and DateTime?

The pandas library provides a DateTime object with nanosecond precision called Timestamp to work with date and time values. The Timestamp object derives from the NumPy’s datetime64 data type, making it more accurate and significantly faster than Python’s DateTime object.

How to find the number of days between two dates Python?

Using Python datetime module: Python comes with an inbuilt datetime module which helps us to solve various datetime related problems. In order to find the difference between two dates we simply input the two dates with date type and subtract them, which in turn provides us the number of days between the two dates.

Where can I learn more about time series data in pandas?

If you’d like to learn more about working with time-series data in pandas, you can check out the Time Series Analysis with Pandas tutorial on the Dataquest blog, and of course, the Pandas documentation on Time series / date functionality. Mehdi is a Senior Data Engineer and Team Lead at ADA.


Video Answer


1 Answers

Assuming your time values are in your index, you can just do this:

import pandas

x = pandas.DatetimeIndex(start='2014-01-01', end='2014-01-06', freq='30T')
df = pandas.DataFrame(index=x, columns=['time since'])

basedate = pandas.Timestamp('2011-11-25')
df['time since'] = df.apply(lambda x: (x.name.to_datetime() - basedate).days, axis=1)

If they're in a column, do:

df['time since'] = df['datetime_column'].apply(lambda x: (x.name.to_datetime() - basedate).days)
like image 126
Paul H Avatar answered Sep 30 '22 17:09

Paul H