简体   繁体   English

计算今天的日期和熊猫日期系列之间的日期差异

[英]calculate date difference between today's date and pandas date series

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.实际上我是 python 的新手,在应用任何函数时都会发生很多语法错误。

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:确保将列名转换为_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 .使用 numpy,您可以像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如果你想要天作为 int 而不是 float 使用

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

From the pandas docs under Converting To Timestamps you will find:Converting To Timestamps下的 pandas 文档中,您会发现:

" Converting to Timestamps To convert a Series or list-like object of date-like objects eg strings, epochs, or a mixture, you can use the to_datetime function " 转换为时间戳要转换类日期对象的系列或类列表对象,例如字符串、纪元或混合,您可以使用to_datetime函数

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.我以前没有使用过熊猫,但这表明您的熊猫日期系列(类似列表的对象)是可迭代的,并且该系列的每个元素都是具有to_datetime函数的类的实例。

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).假设我的假设是正确的,下面的函数将采用这样的列表并返回一个 timedeltas' 列表(一个日期时间对象,表示两个日期时间对象之间的差异)。

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)

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

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