ÖMER ELMASRI
ÖMER ELMASRI

Reputation: 1

How to get the day difference between date-column and maximum date of same column or different column in Python?

I am setting up a new column as the day difference in Python (on Jupyter notebook).

I carried out the day difference between the column date and current day. Also, I carried out that the day difference between the date column and newly created day via current day (Current day -/+ input days with timedelta function).

However, whenever I use max() of the same column and different column, the day difference column has NaN values. It does not make sense for me, maybe I am missing the date type. When I checked the types all of them seems datetime64 (already converted to datetime64 by me).

I thought that the reason was having not big enough date. However, it happens with any specific date like max(datecolumn)+timedelta(days=i).

t=data_signups[["date_joined"]].max()

date_joined 2019-07-18 07:47:24.963450 dtype: datetime64[ns]

t = t + timedelta(30)

date_joined 2019-08-17 07:47:24.963450 dtype: datetime64[ns]

data_signups['joined_to_today'] = (t - data_signups['date_joined']).dt.days

data_signups.head(2)

shortened...

However it worked on Current day task like below.

Currentdate = datetime.datetime.now() print(Currentdate) 2019-09-01 17:05:48.934362

before_days=int(input("Enter the number of days before today for analysis "))

30

Done

last_day_for_analysis = Currentdate - timedelta(days=before_days)

print(last_day_for_analysis)

2019-08-02 17:05:48.934362

data_signups['joined_to_today'] = (last_day_for_analysis - data_signups['date_joined']).dt.days

data_signups.head(2)

shortened...

I expect that there is datetype problem. However, I could not figure out since all of them are datetime64. There are no NaN values in the columns.

Thank you for your help. I am newbie and I try to learn everyday continuously.

Upvotes: 0

Views: 92

Answers (1)

ÖMER ELMASRI
ÖMER ELMASRI

Reputation: 1

Although I was busy with this question for 2 days, now I realized that I had a big mistake. Sorry to everyone.

The reason that can not take the maximum value as date comes from as below.

Existing one: t=data_signups[["date_joined"]].max()

Must-be-One: t=data_signups["date_joined"].max()

So it works with as below.

data_signups['joined_to_today'] = (data_signups['date_joined'].max() - data_signups['date_joined']).dt.days data_signups.head(3)

There will be no two brackets. So stupid mistake. Thank you.

Upvotes: 0

Related Questions