- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有一个文件集合。每个文件有 1 秒数据。此外,这些文件不是周期性的,即它们不是每日文件。例如,一个文件可能包含一天半的数据,而下一个文件可能包含3天2小时的数据;文件之间和文件内部可能存在间隙。另一个问题是同时加载内存中的所有文件是不切实际的。
这是一个显示问题的具体示例。以下数据帧有一天半的 1 秒数据:
index = pd.date_range('now', periods=60*60*24*1.5, freq='1S')
data_a = pd.DataFrame(np.random.rand(len(index)), index=index, columns=['data'])
下一个数据帧从上一个数据帧停止的地方开始,它有两天的数据:
index = pd.date_range(data_a.index[-1] + pd.Timedelta('1S'), periods=60*60*24*2, freq='1S')
data_b = pd.DataFrame(np.random.rand(len(index)), index=index, columns=['data'])
让我们在每个数据帧上创建 10 分钟的迭代器和 chain他们:
ia = iter(data_a.groupby(TimeGrouper('10Min')))
ib = iter(data_b.groupby(TimeGrouper('10Min')))
iaib = chain(ia, ib)
如果我们迭代 iaib
,期望的行为是仅查看每个组键(及其数据)一次,但事实并非如此。
seen = {}
for name, group in iaib:
count = seen.get(name, 0)
seen[name] = count + 1
seen_twice = {key: value for key, value in seen.items() if value > 1}
seen_twice
的内容是:
{Timestamp('2017-06-02 08:50:00', freq='10T'): 2}
在此示例中,2017-06-02 08:50:00
是最后一组 data_a
和第一组 data_b< 的键
.
如何在所有文件上按 10 分钟组进行迭代,而不在文件边缘重复组?
最佳答案
该解决方案分为两部分:一是将所有文件作为单个数据集处理;二是将所有文件作为一个数据集进行处理。另一个是考虑到这样一个事实:10 分钟的组可以分为一个文件的结尾和下一个文件的开始。
这些是所需的导入:
from itertools import chain
import pandas as pd
from pandas.tseries.resample import TimeGrouper
此函数返回给定文件的 10 分钟组的迭代器:
def make_iterator(file):
df = pd.read_csv(file, index_col='timestamp', parse_dates=['timestamp'])
return iter(df.groupby(TimeGrouper('10Min')))
上面的函数用于创建 itertools.chain
的迭代器的迭代器。给定一个文件列表,可以像这样创建文件集合的所有 10 分钟组上的单个迭代器:
files = ... # list obtained by os.listdir() or glob.glob()
iterator_of_single_file_group_iterators = map(make_iterator, files)
chained_file_group_iterator = chain.from_iterable(iterator_of_single_file_group_iterators)
但是,上面的迭代器不知道跨两个文件的 10 分钟组。下面的类解决了这个问题:
class TimeGrouperChainDecorator(object):
def __init__(self, iterator):
self.iterator = iterator
self._has_more = True
self._last_item = next(self.iterator)
def __iter__(self):
return self
def __next__(self):
if not self._has_more:
raise StopIteration
try:
return self._next()
except StopIteration:
self._has_more = False
if self._last_item is not None:
return self._last_item
raise StopIteration
def _next(self):
new_key, new_data = next(self.iterator)
last_key, last_data = self._last_item
if new_key == last_key:
data = pd.concat([last_data, new_data])
try:
self._last_item = next(self.iterator)
except StopIteration:
self._has_more = False
return new_key, data
else:
self._last_item = new_key, new_data
return last_key, last_data
请注意,该实现完全依赖于 pandas groupby
API。要使用它,请使用上述链接的迭代器创建该类的实例:
iterator = TimeGrouperChainDecorator(chained_file_group_iterator)
for name, group in iterator:
# do something with each 10 minute group
我的实现可能并不完美,因此欢迎任何反馈。我发布了snippet with 3 tests .
关于python - 如何使用 TimeGrouper 迭代包含不同范围的多个文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44289149/
我有一个 csv 文件,其中日期作为列标题,二进制矩阵为 1、0 或 np.nan。 我想取每个指数的平均值,按月分组。我遇到了问题,因为我的列不是日期时间索引,我尝试使用 pd.to_datetim
我正在尝试以 3 小时为间隔对具有时间索引的数据帧进行分组。它以 1.5 秒的频率采样。我希望以下内容返回单个长度为 4323 的组。 import pandas as pd time_grouper
TL:DR 我想按主题和 30 天时间段分组,但 30 天时间段并未按主题个性化。 处理此问题的最佳方法是什么? 完整解释 我有一个参与者样本,他们都在不同时间开始了一项科学研究。我想使用 TimeG
如何按自定义时间范围对数据进行分组?我能够按小时对数据进行分组,但我想以 1 小时 1 分钟的间隔对其进行分组。 类似这样的代码:df.groupby(pd.TimeGrouper('1H:1Min'
我有一个文件集合。每个文件有 1 秒数据。此外,这些文件不是周期性的,即它们不是每日文件。例如,一个文件可能包含一天半的数据,而下一个文件可能包含3天2小时的数据;文件之间和文件内部可能存在间隙。另一
我有一个 multiIndex pandas 数据框,其中第一级索引是一个组,第二级索引是时间。我想要做的是,在每个组内,以日内观察的平均值重新采样到每日频率。 import pandas as pd
我经常使用 Pandas,它很棒。我也使用 TimeGrouper,它很棒。我实际上不知道关于 TimeGrouper 的文档在哪里。有吗? 谢谢! 最佳答案 pd.TimeGrouper() 为 f
这是数据: date 2016-03-23 hiit 2016-03-30 hiit 2016-04-20 hiit 2016-08-24 hiit 2016-09-04
我正在尝试以编程方式构建一个 pandas TimeGrouper。快速查看代码会发现 TimeGrouper 的 __init__ 方法的 freq 参数被转换为 DateOffset 通过 to_
根据this问题。当应用于我的 df 的 pd.rolling_mean 列时,此 groupby 会起作用,如下所示: data['maFast']=data['Last'].groupby(pd.
有什么方法可以阻止 pandas.TimeGrouper() 返回不完整的组 (ts1)?目前我正在使用以下内容来确定不完整的组成员的数量,然后使用 .ix 删除这些行 (ts2)。我想知道是否有更好
我想按月份对数据进行分组,选择每个月的最后一行。 数据: >>> df Date 1985-10-14 46.50 1985-10-23 47.50 1985-10-24 46.88
这是一个可重现的例子: from pandas import DataFrame, Timestamp, TimeGrouper i = [Timestamp('2015-10-07 03:50:01
我正在使用 Pandas Timegrouper 在 python 中对 pandas 数据帧中的数据点进行分组: grouped = data.groupby(pd.TimeGrouper('30S
我有数据 i,ID,url,used_at,active_seconds,domain,search_term 322015,0120bc30e78ba5582617a9f3d6dfd8ca,vk.
我有一个包含时间序列的 DataFrame: rng = pd.date_range('2016-06-01', periods=24*7, freq='H') ones = pd.Series([1
这是我的数据框的样子: Timestamp CAT 0 2016-12-02 23:35:28 200 1 2016-12-02 23:37:43 20
我有一个员工工资数据的数据框(示例如下),其中“日期”是指员工工资生效的时间: Employee Date Salary PersonA 1/1/2016 $5000
我正在 Excel 的数据框上使用 Timegrouper,并尝试使用日期作为列标题并使用时间作为行来执行 Pviot,Y 上的聚合计数是“Barton LLC”。 Data.xls X
我有一个这样的时间序列 Time Demand Date 2014-01-01 0:00 2899.0 2014-01-01 0:15 28
我是一名优秀的程序员,十分优秀!