简体   繁体   English

每半小时可以使用熊猫TimeDeltaIndex,PeriodIndex或DateTimeIndex吗?

[英]For half-hourly intervals can I use Pandas TimeDeltaIndex, PeriodIndex or DateTimeIndex?

I have a table of data values that should be indexed with half-hourly intervals, and I've been processing them with Pandas and Numpy. 我有一个数据值表,应该以半小时为间隔编制索引,并且我一直在用Pandas和Numpy处理它们。 Currently they're in CSV files and I import them using read_csv to a dataframe with only the interval-endpoint as an index. 当前它们在CSV文件中,我使用read_csv将它们导入到仅以时间间隔端点为索引的数据帧中。 I am uncomfortable with that and want to have the intervals themselves as the index. 我对此感到不舒服,并希望将间隔本身作为索引。

I do not know whether to use a DateTimeIndex , a PeriodIndex or a TimedeltaIndex ... All of them seem very similar in practice, to me. 我不知道是否要使用DateTimeIndexPeriodIndexTimedeltaIndex ...在我看来,它们在实践中都非常相似。 My operations include 我的操作包括

  • Looking up a particular interval 查找特定间隔
  • Checking if a DateTime is contained in a particular interval 检查DateTime是否包含在特定间隔中
  • Intersection and (Set)Difference of intervals 相交和(Set)间隔的差异
  • Split and join intervals 分割和加入间隔

Can Pandas even do all of these? 熊猫能做到所有这些吗? Is it advisable? 这是明智的吗? I already am using this interval library , would using Pandas tslib and period be better? 我已经在使用此时间间隔库 ,使用Pandas tslibperiod会更好吗?

if you only need a series with time interval of 30 minutes you can do this: 如果您只需要时间间隔为30分钟的系列,则可以执行以下操作:

import pandas as pd
import datetime as dt

today = dt.datetime.date()
yesterday = dt.datetime.date()-dt.timedelta(days=1)
time_range = pd.date_range(yesterday,today, freq='30T')

now you could use it to set an index such has 现在您可以使用它来设置索引

pd.DataFrame(0, index=time_range,columns=['yourcol'])

Out[35]: 
                     yourcol
2016-09-25 00:00:00        0
2016-09-25 00:30:00        0
2016-09-25 01:00:00        0
2016-09-25 01:30:00        0
2016-09-25 02:00:00        0

this would be a DateTimeIndex 这将是一个DateTimeIndex

you can read more about time interval in pandas here: http://pandas.pydata.org/pandas-docs/stable/timeseries.html#offset-aliases 您可以在此处了解有关熊猫时间间隔的更多信息: http : //pandas.pydata.org/pandas-docs/stable/timeseries.html#offset-aliases

暂无
暂无

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

相关问题 pandas 不能按日期分组,仅对 DatetimeIndex、TimedeltaIndex 或 PeriodIndex 有效,但 - pandas cannot group by date, Only valid with DatetimeIndex, TimedeltaIndex or PeriodIndex, but TypeError:仅对 DatetimeIndex、TimedeltaIndex 或 PeriodIndex 有效,但得到了“RangeIndex”的实例,我不知道为什么 - TypeError: Only valid with DatetimeIndex, TimedeltaIndex or PeriodIndex, but got an instance of 'RangeIndex' and I can't figure out why 将Pandas数据框汇总为半小时 - Summarize Pandas dataframe to Half-Hourly Pandas重采样:TypeError:仅对DatetimeIndex,TimedeltaIndex或PeriodIndex有效,但得到'RangeIndex'的实例 - Pandas Resampling: TypeError: Only valid with DatetimeIndex, TimedeltaIndex or PeriodIndex, but got an instance of 'RangeIndex' Pandas TypeError:仅对DatetimeIndex,TimedeltaIndex或PeriodIndex有效,但具有“ Int64Index”的实例 - Pandas TypeError: Only valid with DatetimeIndex, TimedeltaIndex or PeriodIndex, but got an instance of 'Int64Index' Pandas dataframe.resample TypeError '仅对 DatetimeIndex、TimedeltaIndex 或 PeriodIndex 有效,但获得了“RangeIndex”实例 - Pandas dataframe.resample TypeError 'Only valid with DatetimeIndex, TimedeltaIndex or PeriodIndex, but got an instance of 'RangeIndex' 类型错误:仅对 DatetimeIndex、TimedeltaIndex 或 PeriodIndex 有效,但得到了“RangeIndex”的实例 - TypeError: Only valid with DatetimeIndex, TimedeltaIndex or PeriodIndex, but got an instance of 'RangeIndex' 使用熊猫将数日长的数据框拆分为半小时的数据框,并将其另存为csv文件 - Splitting several days long dataframe into half-hourly dataframes using pandas and save them as csv-files 仅平均工作日的半小时数据 - averaging half-hourly data for weekdays only 在 pandas 中使用 PeriodIndex 与 DateTimeIndex? - using PeriodIndex vs DateTimeIndex in pandas?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM