简体   繁体   English

Python Pandas:两周内两个日期之间的差异?

[英]Python Pandas: differences between two dates in weeks?

When trying to find differences between two dates in weeks:当试图在几周内找到两个日期之间的差异时:

import pandas as pd

def diff(start, end):
    x = millis(end) - millis(start)
    return x / (1000 * 60 * 60 * 24 * 7 * 1000)

def millis(s):
    return pd.to_datetime(s).to_datetime64()

diff("2013-06-10","2013-06-16")

As a result I get:结果我得到:

Out[15]: numpy.timedelta64(857,'ns')

Which is obviously wrong.这显然是错误的。 Questions:问题:

  1. How to get the difference in weeks, not nanoseconds, rounded up to a whole value?如何获得以周为单位的差异,而不是纳秒,四舍五入为整数值?

  2. How to get value out of 'numpy.timedelta64' object?如何从“numpy.timedelta64”对象中获取价值?

I think you can convert to int by dividing by numpy scalar:我认为您可以通过除以 numpy 标量来转换为int

def diff(start, end):
    x = pd.to_datetime(end) - pd.to_datetime(start)
    return int(x / np.timedelta64(1, 'W'))

print (diff("2013-06-10","2013-06-16"))
0
print (diff("2013-06-10","2013-06-26"))
2

See frequency conversion .参见变频

You can use pandas.Timedelta as well:您也可以使用pandas.Timedelta

import pandas as pd

def diff(start, end):
    days = pd.to_datetime(end) - pd.to_datetime(start)
    week = int(pd.Timedelta(days).days / 7)
    remainder = pd.Timedelta(days).days % 7
    return str(week) + ' weeks and ' + str(remainder) + ' days'

print(diff("2019-06-10","2019-07-11"))

Output:
4 weeks and 3 days

Here's a simple fix:这是一个简单的修复:

def diff(start, end):
    x = millis(end) - millis(start)
    return np.ceil(x.astype(int) / (7*86400*1e9))

The main thing is to remove the units (nanoseconds) before operating on it.主要是在操作之前删除单位(纳秒)。

PS: Consider not calling your function millis() when it does not return milliseconds. PS:当函数未返回毫秒时,请考虑不要调用您的函数millis()

jezrael's answer threw an error for me so here's an alternate solution (in case you also got an error when trying it) jezrael 的回答给我带来了一个错误,所以这是一个替代解决方案(以防您在尝试时也遇到错误)

import numpy as np
import pandas as pd
def diff(start, end):
    x = pd.to_datetime(end) - pd.to_datetime(start)
    return (x).apply(lambda x: x/np.timedelta64(1,'W')).astype(int)

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

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