如何计算两个日期之间的天数,这些日期分为大熊猫每月的天数

时间:2017-02-26 07:06:56

标签: python pandas datetime

我想在A' A'中删除日期。来自B' B'并获得日期之间每个月的天数差异:

df
      A        B
 2014-01-01  2014-02-28 
 2014-02-03  2014-03-01

df['A'] = pd.to_datetime(df['A'])
df['B'] = pd.to_datetime(df['B'])
#df['A'] - df['B']

Desired Output:
=================
01(Jan)    02(Feb)      03(Mar)
================================
31days     28days       0days
0days      26days       1day     

如何使用熊猫来实现这一目标?

1 个答案:

答案 0 :(得分:2)

有趣的问题,谢谢分享。这里介绍的基本思想是构建一个函数,它可以在开始日期和结束日期之间进行迭代,并返回一个带有年/月密钥的dict,以及该月份的天数值。

<强>代码:

import calendar
import datetime as dt

def year_month(date):
    """ return year/month tuple from date """
    return date.year, date.month

def next_year_month(date):
    """ given a year/month tuple, return the next year/month """
    if date[1] == 12:
        return date[0] + 1, 1
    else:
        return date[0], date[1] + 1

def days_per_month(start_date, end_date):
    """ return dict keyed with year/month tuples and valued with days in month """
    assert isinstance(start_date, (dt.datetime, dt.date))
    assert isinstance(end_date, (dt.datetime, dt.date))

    start = year_month(start_date)
    end = year_month(end_date)
    days_in_month = (
        calendar.monthrange(*start)[1] - start_date.day + 1)

    result = {}
    while start != end:
        result[start] = days_in_month
        start = next_year_month(start)
        days_in_month = calendar.monthrange(*start)[1]
    result[end] = (
        end_date.day - calendar.monthrange(*end)[1] + days_in_month)
    return result

测试代码:

import pandas as pd
data = [x.strip().split() for x in """
        A          B
    2014-01-01  2014-02-28
    2014-02-03  2014-03-01
    2014-02-03  2014-02-05
""".split('\n')[1:-1]]
df = pd.DataFrame(data=data[1:], columns=data[0])
df['A'] = pd.to_datetime(df['A'])
df['B'] = pd.to_datetime(df['B'])

result = pd.DataFrame.from_records(
    (days_per_month(a, b) for a, b in zip(df['A'], df['B']))
).fillna(0).astype(int)

print(result)

<强>结果:

   (2014, 1)  (2014, 2)  (2014, 3)
0         31         28          0
1          0         26          1
2          0          3          0