简体   繁体   English

如何获取Python中同一列或不同列的日期列和最大日期之间的日差?

[英]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). 我正在设置新列作为Python中的日差(在Jupyter笔记本上)。

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). 此外,我还执行了日期列和通过当前日期新创建的日期之间的日期差(使用timedelta函数的“当前日期-/ +输入日期”)。

However, whenever I use max() of the same column and different column, the day difference column has NaN values. 但是,每当我使用同一列和不同列的max()时,日差列都具有NaN值。 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). 当我检查所有类型时,它们似乎都是datetime64(我已经将其转换为datetime64)。

I thought that the reason was having not big enough date. 我以为原因没有足够大的日期。 However, it happens with any specific date like max(datecolumn)+timedelta(days=i). 但是,它发生在任何特定日期,例如max(datecolumn)+ timedelta(days = i)。

t=data_signups[["date_joined"]].max() t = data_signups [[“” date_joined“]]。max()

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

t = t + timedelta(30) t = t + timedelta(30)

date_joined 2019-08-17 07:47:24.963450 dtype: datetime64[ns] 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 ['joined_to_today'] =(t-data_signups ['date_joined'])。dt.days

data_signups.head(2) data_signups.head(2)

shortened... 缩短...

  • date_joined_______________// joined_to_today________ date_joined _______________ // join_to_today_________
  • 2019-05-31 10:52:06.327341 // nan 2019-05-31 10:52:06.327341 //楠
  • 2019-04-02 09:20:26.520272 // nan 2019-04-02 09:20:26.520272 //楠

However it worked on Current day task like below. 但是,它可以像下面这样处理当日任务。

Currentdate = datetime.datetime.now() print(Currentdate) 2019-09-01 17:05:48.934362 当前日期= datetime.datetime.now()打印(当前日期)2019-09-01 17:05:48.934362

before_days=int(input("Enter the number of days before today for analysis ")) before_days = int(input(“输入今天进行分析的天数”))

30 30

Done 完成

last_day_for_analysis = Currentdate - timedelta(days=before_days) last_day_for_analysis =当前日期-timedelta(天= before_days)

print(last_day_for_analysis) 打印(last_day_for_analysis)

2019-08-02 17:05:48.934362 2019-08-02 17:05:48.934362

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

data_signups.head(2) data_signups.head(2)

shortened... 缩短...

  • date_joined_______________// joined_to_today________ date_joined _______________ // join_to_today_________
  • 2019-05-31 10:52:06.327341 // 63 2019-05-31 10:52:06.327341 // 63
  • 2019-04-02 09:20:26.520272 // 122 2019-04-02 09:20:26.520272 // 122

I expect that there is datetype problem. 我希望有日期类型问题。 However, I could not figure out since all of them are datetime64. 但是,我都不知道,因为它们都是datetime64。 There are no NaN values in the columns. 列中没有NaN值。

Thank you for your help. 谢谢您的帮助。 I am newbie and I try to learn everyday continuously. 我是新手,我每天都在不断学习。

Although I was busy with this question for 2 days, now I realized that I had a big mistake. 尽管我忙于这个问题2天,但现在我意识到自己犯了一个大错误。 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() 现有的一个:t = data_signups [[“” date_joined“]]。max()

Must-be-One: t=data_signups["date_joined"].max() 必须为一: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) 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. 谢谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM