1. 案例1
data_27 = pd.read_csv(r'E:\02-data\\0329_1556.csv', encoding='gbk', parse_dates=['TIMESTAMP'])
data_103 = pd.read_csv(r'E:\02-data\\0329_1545.csv',
encoding='gbk', parse_dates=['TIMESTAMP'])
data_27v1 = data_27.set_index('TIMESTAMP', drop=True)
data_103v1 = data_103.set_index('TIMESTAMP', drop=True)
df_Tai_27 = data_27v1.resample('10min')
nr_in_27 = df_Tai_27.resample('10min')['WS_103m_x'].aggregate(len)
Traceback (most recent call last):
File "D:\python3.8.5\lib\site-packages\IPython\core\interactiveshell.py", line 3418, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-2-4a953e0756bb>", line 4, in <module>
nr_in_27 = df_Tai_27.resample('10min')['WS_103m_x'].aggregate(len)
File "D:\python3.8.5\lib\site-packages\pandas\core\resample.py", line 199, in __getattr__
return object.__getattribute__(self, attr)
AttributeError: 'DatetimeIndexResampler' object has no attribute 'resample'
AttributeError: 'DatetimeIndexResampler' object has no attribute 'resample'
。
主要错误发生在
nr_in_27 = df_Tai_27.resample('10min')['WS_103m_x'].aggregate(len)
# 求每个分仓段内数据的数量(多少行)
修改办法:
df.index = pd.DatetimeIndex(df.TimeStamp)
data_27v1 = data_27.copy()
data_27v1.index = pd.DatetimeIndex(data_27v1.TIMESTAMP)
df_Tai_27 = data_27v1.resample('10min')['WS_27m_x'].aggregate(len)
2. 案例2
分析下面的bug
Tai = 10
time_interval = '10min'
df_Tai = df.resample(time_interval)
df_Tai.loc['2020-10-16 12:00:00']
Python 3.8.5 (default, Sep 3 2020, 21:29:08) [MSC v.1916 64 bit (AMD64)]
Type 'copyright', 'credits' or 'license' for more information
IPython 7.19.0 -- An enhanced Interactive Python. Type '?' for help.
PyDev console: using IPython 7.19.0
Traceback (most recent call last):
File "D:\Anaconda\lib\site-packages\IPython\core\interactiveshell.py", line 3418, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-1-3960fa84ae93>", line 1, in <module>
df_Tai.loc['2020-10-16 12:00:00']
File "D:\Anaconda\lib\site-packages\pandas\core\resample.py", line 199, in __getattr__
return object.__getattribute__(self, attr)
AttributeError: 'DatetimeIndexResampler' object has no attribute 'loc'
在错误信息中,bug是由于尝试直接在 DatetimeIndexResampler
对象上使用 .loc
属性造成的。resample
方法返回的是一个 Resampler
对象,而不是一个直接可以进行索引或者使用 .loc
的 DataFrame。你需要先对 Resampler
对象进行一些聚合操作(例如 mean
, sum
, count
等),这样才会生成一个新的 DataFrame 或者 Series 对象,之后你才能使用 .loc
或其他类似的方法。
这里是如何修复这个错误的步骤:
- 在使用
.loc
之前,先对df_Tai
进行一个聚合操作。 - 确保使用正确的时间字符串格式,与数据中的索引格式匹配。
修正的代码示例:
import pandas as pd
import numpy as np
# 假设 df 已经定义并且具有时间戳索引
# 示例数据和 DataFrame 的创建
rng = pd.date_range('2020-10-16 12:00:00', periods=100, freq='T')
df = pd.DataFrame({ 'Data': np.random.randn(len(rng)) }, index=rng)
# 设置重采样的时间间隔为10分钟
time_interval = '10T'
# 对 DataFrame 进行重采样,并计算每个时间段的平均值
df_Tai = df.resample(time_interval).mean()
# 使用.loc访问特定时间点
specific_data = df_Tai.loc['2020-10-16 12:00:00']
print(specific_data)
在这段代码中,.resample(time_interval).mean()
首先对原始的时间序列数据进行每10分钟的重采样,然后计算每个时间段内的平均值,从而返回一个新的 DataFrame。之后,可以使用 .loc['2020-10-16 12:00:00']
来访问特定时间点的数据。
总结,错误的根本原因是 resample
返回的对象不是 DataFrame,而是需要进一步处理(通过聚合函数)来生成 DataFrame 或 Series。这个步骤完成后,才能使用 .loc
或其他 DataFrame/Series 的方法。