0

In my script, I use pandas module. When I execute my file.py - everything works well. But I've converted my file.py to file.exe with auto-py-to-exe and got an error: AttributeError:'str' object has no attribute 'unique'. It's strange because it worked normally. The line where becomes an error: wells=list(file[0].unique()). Who knows this issue, please help.

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

  • Please don't post images of code/error messages/data as images. Post the text directly here on SO. It seems, `file[0]` contains a string like `"ABC"`, so `"ABC".unique()` is not defined. How to address this problem depends on your code that you did not include in the question. – Mr. T Jan 20 '22 at 11:22
  • Thanks for your comment, file is DataFrame where file[0] is the first column (I can't understand how to add my code here right) – Aleksandr Tyshkevich Jan 20 '22 at 11:27
  • @Mr.T please look at [type of file[0]] image – Aleksandr Tyshkevich Jan 20 '22 at 11:46
  • Well, we can guess all day long. Please read [How to make good reproducible pandas examples](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) and [How to create a Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) and update your question with a reproducible example. – Mr. T Jan 20 '22 at 11:49
  • @Mr.T added, look – Aleksandr Tyshkevich Jan 20 '22 at 12:01
  • @Mr.T when I tried to replace on .iloc[:, 0] I got an error: AttributeError: 'str' object has no attribute 'iloc'. I can't understand why it's regarded as str but not a Series – Aleksandr Tyshkevich Jan 20 '22 at 12:25
  • Are you sure that you did not accidentally change the variable order like `name, file = open()` when calling the function? I still cannot reproduce the error with the information provided. – Mr. T Jan 20 '22 at 12:28

1 Answers1

0

When I created virtual env I should have added openpyxl module. And I made it and everything is fine now