【问题标题】:AttributeError:'str' object has no attribute 'unique' (Pandas.unique)AttributeError:'str' 对象没有属性 'unique' (Pandas.unique)
【发布时间】:2022-01-20 11:18:00
【问题描述】:

在我的脚本中,我使用 pandas 模块。当我执行我的 file.py - 一切正常。但是我已经使用 auto-py-to-exe 将我的 file.py 转换为 file.exe 并得到了一个错误:AttributeError:'str' object has no attribute 'unique'。这很奇怪,因为它正常工作。成为错误的行:wells=list(file[0].unique())。谁知道这个问题,请帮忙。

import tkinter as tk
import tkinter.filedialog as fd
import pandas as pd
import os
import datetime
from datetime import datetime, date
import numpy as np
pd.set_option('display.notebook_repr_html', False)
pd.set_option('display.max_columns', 80)
pd.set_option('display.max_rows', 200)
pd.set_option('display.width', 800)

def resource_path(relative_path):
    try:
        base_path = sys._MEIPASS
    except Exception:
        base_path = os.path.abspath(".")
    return os.path.join(base_path, relative_path)

def open():
    global file_excel, name
    file_excel = fd.askopenfilename(initialdir='/Desktop',     title='Открыть файл', filetypes = [("Excel", "*.xlsx")])
    name = os.path.basename(file_excel)
    name=os.path.splitext(name)[0]
    file_excel=pd.read_excel(file_excel, skiprows=[0], header=None)
    win.destroy()
    return file_excel, name

win = tk.Tk()
path = resource_path("image.png")
photo = tk.PhotoImage(file=path)
win.iconphoto(False, photo)
win.config(bg='#FFC')
win.title('Конвертация в формат .ev')
win.geometry('400x130+500+500')
win.resizable(False, False)

label_1 = tk.Label(win, text = 'Выберите файл с испытаниями скважин:',
                  bg = '#FFC',
                  font=('Arial', 10, 'bold'),
                  padx=20,
                  pady=10).pack()
btn_1 = tk.Button(win, text = 'Выбрать Excel', 
                 command = open,
                 activebackground = '#6F6',
                 font=('Arial', 12, 'bold'),
                 padx=20,
                 pady=10,  
                 relief = tk.RAISED,
                 bd=2).pack()
win.mainloop()

wells=list(file_excel[0].unique())
file_excel[1] = pd.to_datetime(file_excel[1],   errors='coerce').dt.strftime("%d/%m/%Y")
file_excel[4] = np.where(file_excel[1].str, 'Perforation', np.nan)
file_excel.iloc[:,[2,3]]=file_excel.iloc[:,[2,3]].abs()
col_list = list(file_excel)
col_list[4], col_list[2] = col_list[2], col_list[4]
file_excel.columns = col_list
Perforation=pd.DataFrame(data=None)
for i in wells:
    well_name=pd.DataFrame({'WELLNAME '+i}, columns=[1])
    Perforation=Perforation.append(well_name)
    Perforation=Perforation.append(file_excel.iloc[:,[1,2,3,4]][file_excel.iloc[:,0]==i])
    Perforation=Perforation.append(pd.Series(dtype = 'object'),     ignore_index=True)

def SaveFile():
    Save=fd.asksaveasfile(mode='w',defaultextension=".ev", initialfile=name)
    Save.write(Perforation.to_string(index=False, header=False, na_rep=' '))
    win.destroy()

win = tk.Tk()
path = resource_path("image.png")
photo = tk.PhotoImage(file=path)
win.iconphoto(False, photo)
win.config(bg='#FFC')
win.title('Конвертация в формат .ev')
win.geometry('400x130+500+500')
win.resizable(False, False)

label_1 = tk.Label(win, text = 'Сохранение:',
                  bg = '#FFC',
                  font=('Arial', 10, 'bold'),
                  padx=20,
                  pady=10).pack()
btn_1 = tk.Button(win, text = 'Сохранить как', 
                 command = SaveFile,
                 activebackground = '#6F6',
                 font=('Arial', 12, 'bold'),
                 padx=20,
                 pady=10,  
                 relief = tk.RAISED,
                 bd=2).pack()
win.mainloop()

type of file[0]

Error screen

【问题讨论】:

  • 请不要将代码/错误消息/数据的图像作为图像发布。将文本直接发布在 SO 上。看来,file[0] 包含一个类似"ABC" 的字符串,所以没有定义"ABC".unique()。如何解决此问题取决于您未在问题中包含的代码。
  • 感谢您的评论,file 是 DataFrame,其中 file[0] 是第一列(我不明白如何在此处添加我的代码)
  • @Mr.T 请看[文件类型[0]]图片
  • 好吧,我们可以猜一整天。请阅读How to make good reproducible pandas examplesHow to create a Minimal, Reproducible Example 并使用可重现的示例更新您的问题。
  • @Mr.T 添加,看

标签: pandas pyinstaller exe series attributeerror


【解决方案1】:

当我创建虚拟环境时,我应该添加 openpyxl 模块。我成功了,现在一切都很好

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-10
    • 2021-10-04
    • 2019-12-02
    • 2021-09-25
    • 2014-03-04
    • 2013-09-22
    相关资源
    最近更新 更多