【问题标题】:TypeError: cannot do slice indexing on <class 'pandas.core.indexes.datetimes.DatetimeIndex'> with these indexers [0.0] of <class 'float'>TypeError:无法使用 <class 'float'> 的这些索引器 [0.0] 对 <class 'pandas.core.indexes.datetimes.DatetimeIndex'> 进行切片索引
【发布时间】:2020-09-13 14:47:37
【问题描述】:

这是我的代码使用流光

def main():

st.title(
    'Aplikasi Forecasting Exchange Rate')

def file_selector(folder_path='./datasets'):

    filenames = os.listdir(folder_path)
    selected_filename = st.selectbox("Select A file", filenames)
    return os.path.join(folder_path, selected_filename)

filename = file_selector()
st.info("Kamu memilih {}".format(filename))

# Read Data
dateparse = lambda dates: pd.datetime.strptime(dates,'%d-%m-%y')
akhir = pd.read_csv(filename, index_col=[0], date_parser=dateparse)
akhir['Terakhir'] = akhir['Terakhir'].str.replace(',', '').astype(float)

# Show Dataset
if st.checkbox("Show Dataset"):
    number = st.number_input("Number of Rows to View")
    st.dataframe(akhir.head(number))

当我想显示数据集时出现错误 TypeError: cannot do slice indexing on with these indexers [0.0] of

【问题讨论】:

  • number 可能是一个浮点数。如果将其转换为 int 会发生什么?

标签: python streamlit


【解决方案1】:

这是因为 number_input 的默认值使用 0.01 的步长并且您没有指定任何函数参数,这意味着返回了一个浮点数。

修复您的代码:

number = st.number_input("Number of Rows to View", min_value = 0, step = 1)

https://docs.streamlit.io/en/latest/api.html#streamlit.number_input

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-05
    • 1970-01-01
    • 2018-01-09
    相关资源
    最近更新 更多