“DataFrame.at[source]:TypeError:仅整数标量数组可以转换为标量索引”是什么意思?

问题描述 投票:0回答:1

搜索“dataframe.at TypeError:只有整数标量数组可以转换为标量索引”的答案,结果是“我们找不到 dataframe.at typeerror 的任何内容:只有整数标量数组可以转换为标量索引” .

使用不太严格的规则进行搜索产生的结果要求我在这些结果中搜索“.at”,也没有产生任何结果。当然,这可能是由于我的搜索词所致。

任何人都可以用非程序员可以理解的术语准确解释这意味着什么吗?

“property DataFrame.at[源]:TypeError:仅整数标量数组 可以转换为标量索引”

pandas 网站声明如下:

pandas.DataFrame.at 属性 DataFrame.at[source] 访问单个 行/列标签对的值。

这看起来很明显,因此很容易使用。但是,以下代码片段:

col = str(currentyr)
print('col = ' + str(col))
row = str(currentmonth) + 'UTtotal'
print('row = ' + str(row))
DTduration = df_total.at[row, col]
print('row / col (' + str(row) + ' / ' + str(col) + ') =\n' + str(DTduration))

产生以下错误:

"TypeError                                 Traceback (most recent call last)
Cell In[1], line 269
    266     monthlyUT()
    268     #deal with monthly downtimes
--> 269     monthlyDT(faultindexfirst, faultindexlast)
    271     framenum += 1
    273 elif framestartyear == currentyr & frameendyear == currentyr + 1:
    274         #the currently selected frame straddles two years

Cell In[1], line 46
     44     print('row = ' + str(row))
     45 #    DTduration = df_total.loc[row, col]
---> 46     DTduration = df_total.at[row, col]
     47     print('row / col (' + str(row) + ' / ' + str(col) + ') =\n' + str(DTduration))
     48     row = str(currentmonth) + 'Eventstotal'"

faultindexfirst、faultindexlast、currentyr 和 currentmonth 都是整数。我正在尝试访问数据框中的特定数据:

                      2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023  \
UTtotal        2311.933333  NaN  NaN  NaN  NaN  NaN  NaN  NaN  NaN  NaN  NaN   
DTtotal                NaN  NaN  NaN  NaN  NaN  NaN  NaN  NaN  NaN  NaN  NaN   
Eventstotal            NaN  NaN  NaN  NaN  NaN  NaN  NaN  NaN  NaN  NaN  NaN   
1UTtotal               NaN  NaN  NaN  NaN  NaN  NaN  NaN  NaN  NaN  NaN  NaN   
1DTtotal               NaN  NaN  NaN  NaN  NaN  NaN  NaN  NaN  NaN  NaN  NaN   
1Eventstotal           NaN  NaN  NaN  NaN  NaN  NaN  NaN  NaN  NaN  NaN  NaN   
.
.
.
11Eventstotal          NaN  NaN  NaN  NaN  NaN  NaN  NaN  NaN  NaN  NaN  NaN   
12UTtotal              NaN  NaN  NaN  NaN  NaN  NaN  NaN  NaN  NaN  NaN  NaN   
12DTtotal              NaN  NaN  NaN  NaN  NaN  NaN  NaN  NaN  NaN  NaN  NaN   
12Eventstotal          NaN  NaN  NaN  NaN  NaN  NaN  NaN  NaN  NaN  NaN  NaN   

在这种情况下,我需要用有效数据填充此表,但 pandas 网站没有提供我足够理解的信息来解决此错误。打印语句成功输出以下内容:

col = 2013
row = 9UTtotal

最小可重复示例:

import pandas
import os
import numpy as np
from pathlib import Path
#import matplotlib.pyplot as plt
#import matplotlib.dates as mdates
#from matplotlib.patches import Rectangle
from datetime import date
from datetime import time
from datetime import datetime
import array

def monthlyDT(firstidx, lastidx):
    #this routine handles the monthly downtime data totaling and copying to the table
    print('entered downtimes()')
    print('\tthe first downtime timestamp = ' + str(DTs[firstidx]))
    print('\tthe last downtime timestamp = ' + str(DTs[lastidx]))
    
    #retrieve the current monthly DT duration and number of events totals from the table
    print('row indices:\n' + str(df_total.index))
    print('column headers: \n' + str(df_total.columns))

    col = str(currentyr)
    print('col = ' + str(col))
    row = str(currentmonth) + 'UTtotal'
    print('row = ' + str(row))
    DTduration = df_total.at[row, col]
    print('row / col (' + str(row) + ' / ' + str(col) + ') =\n' + str(DTduration))
    return()


#setup working arrays to hold the datasets
DTs = np.array([1.378854180000000000e+09, 1.378904520000000000e+09, 1.378957920000000000e+09, 1.378968180000000000e+09])
DTe = np.array([1.378858140000000000e+09, 1.378908000000000000e+09, 1.378958040000000000e+09, 1.378968240000000000e+09])

#build the 'year' column names array
colname = []
y = 2013
currentyr = 2013
yrend = 2024
currentmonth = 9
while y <= yrend:
    colname.append(str(y))
    y = y + 1
print('colname = ' + str(colname))
#create the index name array
indexnames = ['UTtotal', 'DTtotal', 'Eventstotal', 
            '1UTtotal', '1DTtotal', '1Eventstotal',
            '2UTtotal', '2DTtotal', '2Eventstotal',
            '3UTtotal', '3DTtotal', '3Eventstotal',
            '4UTtotal', '4DTtotal', '4Eventstotal',
            '5UTtotal', '5DTtotal', '5Eventstotal',
            '6UTtotal', '6DTtotal', '6Eventstotal',
            '7UTtotal', '7DTtotal', '7Eventstotal',
            '8UTtotal', '8DTtotal', '8Eventstotal',
            '9UTtotal', '9DTtotal', '9Eventstotal',
            '10UTtotal', '10DTtotal', '10Eventstotal',
            '11UTtotal', '11DTtotal', '11Eventstotal',
            '12UTtotal', '12DTtotal', '12Eventstotal'
            ]

#create dataframe
df_total = pandas.DataFrame(columns=[colname], index=[indexnames], dtype=np.float64)
print('df_total is:\n' + str(df_total))
df_total = df_total.fillna(-1)
print('\n\ndf_total is:\n' + str(df_total) + '\n\n')

monthlyDT(0, 3)
print('end')
python pandas dataframe
1个回答
0
投票

这是由一个非常无害的打字错误引起的。 在这一行中:

df_total = pandas.DataFrame(columns=[colname], index=[indexnames], dtype=np.float64)

colname
indexnames
已经是列表了。 您已将它们放入列表的列表中,这使得列标题和行标题都成为多重索引。 只需删除括号即可:

df_total = pandas.DataFrame(columns=colname, index=indexnames, dtype=np.float64)
© www.soinside.com 2019 - 2024. All rights reserved.