Pandas 中的日期范围

Date ranges in Pandas(Pandas 中的日期范围)
本文介绍了Pandas 中的日期范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

在与 NumPy 和 dateutil 斗争了几天之后,我最近发现了神奇的 Pandas 库.我一直在研究文档和源代码,但我不知道如何让 date_range() 在正确的断点处生成索引.

After fighting with NumPy and dateutil for days, I recently discovered the amazing Pandas library. I've been poring through the documentation and source code, but I can't figure out how to get date_range() to generate indices at the right breakpoints.

from datetime import date
import pandas as pd

start = date('2012-01-15')
end = date('2012-09-20')
# 'M' is month-end, instead I need same-day-of-month
date_range(start, end, freq='M')

我想要什么:

2012-01-15
2012-02-15
2012-03-15
...
2012-09-15

我得到了什么:

2012-01-31
2012-02-29
2012-03-31
...
2012-08-31

我需要一个月大小的块来说明一个月中的可变天数.这可以通过 dateutil.rrule 实现:

I need month-sized chunks that account for the variable number of days in a month. This is possible with dateutil.rrule:

rrule(freq=MONTHLY, dtstart=start, bymonthday=(start.day, -1), bysetpos=1)

丑陋且难以辨认,但它有效.我怎么能用熊猫做到这一点?我玩过 date_range()period_range(),到目前为止都没有运气.

Ugly and illegible, but it works. How can do I this with pandas? I've played with both date_range() and period_range(), so far with no luck.

我的实际目标是使用 groupbycrosstab 和/或 resample 根据 sums/means/etc 计算每个时期的值期间内的个别条目.换句话说,我想从以下位置转换数据:

My actual goal is to use groupby, crosstab and/or resample to calculate values for each period based on sums/means/etc of individual entries within the period. In other words, I want to transform data from:

                total
2012-01-10 00:01    50
2012-01-15 01:01    55
2012-03-11 00:01    60
2012-04-28 00:01    80

#Hypothetical usage
dataframe.resample('total', how='sum', freq='M', start='2012-01-09', end='2012-04-15') 

                total
2012-01-09          105 # Values summed
2012-02-09          0   # Missing from dataframe
2012-03-09          60
2012-04-09          0   # Data past end date, not counted

鉴于 Pandas 最初是一种财务分析工具,我几乎可以肯定有一种简单快捷的方法可以做到这一点.感谢您的帮助!

Given that Pandas originated as a financial analysis tool, I'm virtually certain that there's a simple and fast way to do this. Help appreciated!

推荐答案

freq='M' 用于月末频率(参见 这里).但是您可以使用 .shift 将其移动任意天数(或任何频率):

freq='M' is for month-end frequencies (see here). But you can use .shift to shift it by any number of days (or any frequency for that matter):

pd.date_range(start, end, freq='M').shift(15, freq=pd.datetools.day)

这篇关于Pandas 中的日期范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

python arbitrarily incrementing an iterator inside a loop(python在循环内任意递增迭代器)
Joining a set of ordered-integer yielding Python iterators(加入一组产生 Python 迭代器的有序整数)
Iterating over dictionary items(), values(), keys() in Python 3(在 Python 3 中迭代字典 items()、values()、keys())
What is the Perl version of a Python iterator?(Python 迭代器的 Perl 版本是什么?)
How to create a generator/iterator with the Python C API?(如何使用 Python C API 创建生成器/迭代器?)
Python generator behaviour(Python 生成器行为)