我如何计算熊猫每月分为几天之间的两个日期之间的天数



我想从'b'中的'a'中减去日期,并在日期之间每月获得每个月的差异:

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     

如何使用熊猫?

有趣的问题,感谢您的分享。这里提出的基本思想是构建一个可以在开始日期和结束日期之间迭代的函数,并返回一年/月的键以及该月的天数的值。

代码:

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

最新更新