【问题标题】:Python Pandas - Can Dataframe have multiple indexes?Python Pandas - Dataframe 可以有多个索引吗?
【发布时间】:2017-06-20 18:39:02
【问题描述】:

我有一个 CSV 格式的数据集,我用它来阅读:

df = pd.read_csv(requestfile, header=[0,1], parse_dates= [0])

以下 Dataframe 采用以下格式 [0..8759]:

                   time output direct diffuse temperature
                 UTC     kW  kW/m2   kW/m2       deg C
0    2014-01-01 00:00:00  0.000  0.000   0.000       1.495
1    2014-01-01 01:00:00  0.000  0.000   0.000       1.543
2    2014-01-01 02:00:00  0.000  0.000   0.000       1.517

现在我想使用 https://github.com/renewables-ninja/gsee (gsee.pv.run_plant_model) 来处理它,但是我收到以下错误:

File "C:\Data\Solar\gsee-master\gsee\trigon.py", line 183, in aperture_irradiance
sunrise_set_times = sun_rise_set_times(direct.index, coords)

File "C:\Data\Solar\gsee-master\gsee\trigon.py", line 56, in sun_rise_set_times
dtindex = pd.DatetimeIndex(datetime_index.to_series().map(pd.Timestamp.date).unique())

File "C:\Users\XX\Anaconda3\lib\site-packages\pandas\core\series.py", line 2177, in map
new_values = map_f(values, arg)

File "pandas\src\inference.pyx", line 1207, in pandas.lib.map_infer (pandas\lib.c:66124)
TypeError: descriptor 'date' requires a 'datetime.datetime' object but received a 'int'

所以我假设错误出在我的默认索引中,所以我修改了 CSV 读取以使用“时间”列作为索引:

df = pd.read_csv(requestfile, header=[0,1], index_col=0, parse_dates= [0])

time                output direct diffuse temperature
UTC                     kW  kW/m2   kW/m2       deg C
2014-01-01 00:00:00  0.000  0.000   0.000       1.495
2014-01-01 01:00:00  0.000  0.000   0.000       1.543

现在我得到的错误如下:

File "C:\Users\XX\Anaconda3\lib\site-packages\pandas\core\frame.py", line 402, in _init_dict
return _arrays_to_mgr(arrays, data_names, index, columns, dtype=dtype)

File "C:\Users\XX\Anaconda3\lib\site-packages\pandas\core\frame.py", line 5398, in _arrays_to_mgr
index = extract_index(arrays)

File "C:\Users\XX\Anaconda3\lib\site-packages\pandas\core\frame.py", line 5437, in extract_index
raise ValueError('If using all scalar values, you must pass'

ValueError: If using all scalar values, you must pass an index

所以如果我理解正确,第一个错误是因为我的索引只是 INT 中的数字 [0..8759],而它应该是日期时间格式,而我的第二个错误是因为我的索引是日期时间格式并且

index = extract_index(arrays)

没有原始索引 [0..8759]。还是我完全理解错误的标量值错误? DataFrame 是否可以有 2 个索引,一个 [0..8759] 和另一个 ['time'] 列?这将如何转换为 pd.read_csv 函数或通过其他方法?

如果有任何帮助,我还对 DataFrame 执行以下操作(当我调用 DataFrame df 时不会显示一些初学者错误)(但它们由 run_plant_model 函数使用):

df.global_horizontal = df.direct + df.diffuse
df.diffuse_fraction = df.diffuse / df.global_horizontal
df.diffuse_fraction = df.diffuse_fraction.fillna(0)

编辑:我现在正确地将最新的列添加到数据框中。它对错误没有任何影响。

函数调用:

gsee.pv.run_plant_model(df, site.coords, angle, azimuth, tracking, 
                        capacity, technology, system_loss, 
                        angles=None, include_raw_data=False)    

我相信最初的问题可能很糟糕:

C:\Users\XX\Anaconda3\lib\site-packages\pandas\indexes\base.py:2683: RuntimeWarning: Cannot compare type 'Timestamp' with type 'str', sort order is undefined for incomparable objects
return this.join(other, how=how, return_indexers=return_indexers)

所以我有 'str' 我应该有 'Timestamp'?

【问题讨论】:

  • 您能否将调用添加到引发错误的函数?

标签: python csv pandas indexing dataframe


【解决方案1】:

好的,我发现了错误,原来的问题很糟糕:

解决方案:

df = pd.read_csv(requestfile, index_col=[0], parse_dates=[0], skiprows=[1])

标题被忽略了,我添加了 read_csv 以跳过包含“str”中单元的行。所以问题是使用的函数之一是试图将“时间戳”与单位行(“str”)进行比较。

【讨论】:

    猜你喜欢
    • 2018-09-07
    • 2019-04-13
    • 2021-04-20
    • 2018-04-08
    • 2021-04-20
    • 2020-01-21
    • 1970-01-01
    • 2018-02-15
    • 2017-06-11
    相关资源
    最近更新 更多