1

I am writing a small script to help some colleagues with a excel sheet that they maintain, I am using the Pandas library as I have many times beore.

This time I am getting an error that numpy has no attribute float.

I have checked and see that numpy has depreciated some code. I have also checked, and people are still using Pandas without issue.

I have run pip install pandas --upgrade to confirm I was running the latest and also added the code to my script:

print("Pandas version:", pd.__version__)
print("Pandas installation path:", pd.__file__)

Which returned that I am using pandas 1.5.3, and is installed where I expected it to be.

I have also upgraded numpy and confirmed to be running the latest version.

So I am unsure what I should be doing next to solve this problem.

Here is my current code:

import pandas as pd
import os
import sys
from datetime import datetime, timedelta



def main():



    print("Pandas version:", pd.__version__)
    print("Pandas installation path:", pd.__file__)

    show_Folder = input("Please provide file path of folder: ")
    #prepared_by = input("Bid prepared by: ")
    show_code = input("Please provide show code: ")

    #Make new folder for summary to be saved.
    today = datetime.now()
    new_folder_name = "A"
    summary_location = get_current(show_Folder)
    summary_template = summary_location + "/summary_template.xlsx"
    new_folder = str(summary_location +"/") + str(today.strftime('%Y%m%d') + new_folder_name )
    while os.path.exists(new_folder):
        # Add the next letter of the alphabet to the end of the folder name
        new_folder_name = chr(ord(new_folder_name[-1])+1)
        new_folder = new_folder[0:-1] + new_folder_name
    try:
        os.mkdir(new_folder)
        print("mkdir Executed")
    except:
        print("Error: Could not create folder")
        sys.exit()



    #get summary template location, save a new file in dated folder save new locaiotn as variable for make_summary.
    print("*** Loading Summary Template *** ")
    template = show_Folder +"/PRODUCTION_RESOURCES/"+"summary_template.xlsx"
    shutil.copy(template, new_folder)
    os.rename(new_folder+"/summary_template.xlsx", new_folder +"/"+ show_code+ "_SeasonSummary_" + str(today.strftime('%Y%m%d')+".xlsx"))
    seasonSummary = new_folder +"/"+ show_code + "_Summary_" + str(today.strftime('%Y%m%d'))+".xlsx"

    #loop through bid files
    episodes, file_paths = get_bids(show_Folder)
    print("There are:", episodes, "Bids in CURRENT folders")
    i = 0
    for path in file_paths:
        make_summary(path,seasonSummary,i)
        print(path)
        i += 1


def get_current(root_folder):
    for dirpath, dirnames, filenames in os.walk(root_folder):
        if 'CURRENT' in dirnames:
            current_path = os.path.join(dirpath, 'CURRENT')
            if os.path.relpath(current_path, root_folder).count(os.sep) == 1:
                return current_path
    return None


def make_summary(file_path, target_file_path, offset):

    # read the source Excel file
    source_data = pd.read_excel(file_path, sheet_name=["Coverpage", "Brkdwn"])

    # extract the data from the specified cells
    data = [
        source_data["Brkdwn"].iloc[3:, 9],
        source_data["Brkdwn"].iloc[4, 27],
        source_data["Brkdwn"].iloc[8, 21],
        source_data["Brkdwn"].iloc[5, 27],
        source_data["Coverpage"].iloc[18, 7],
        source_data["Coverpage"].iloc[22, 8],
        source_data["Coverpage"].iloc[22, 7],
        source_data["Brkdwn"].iloc[7, 9],
        source_data["Brkdwn"].iloc[8, 9],
        source_data["Brkdwn"].iloc[8, 9],
        source_data["Coverpage"].iloc[28, 7],
    ]

    # read the target Excel file
    target_data = pd.read_excel(target_file_path)

    # calculate the row number based on the offset
    row_num = 14 + offset

    # write the data to the target file at the appropriate locations
    target_data.iloc[row_num, 14] = data[0].to_list()
    target_data.iloc[row_num, 15] = data[1]
    target_data.iloc[row_num, 16] = data[2]
    target_data.iloc[row_num, 17] = data[3]
    target_data.iloc[row_num, 18] = data[4]
    target_data.iloc[row_num, 19] = data[5]
    target_data.iloc[row_num, 20] = data[6]
    target_data.iloc[row_num, 21] = data[7]
    target_data.iloc[row_num, 22] = data[8]
    target_data.iloc[row_num, 23] = data[9]
    target_data.iloc[row_num, 24] = data[10]

    # save the updated target file
    target_data.to_excel(target_file_path, index=False)


def get_bids(root_folder):
    episodes = 0
    file_paths = []
    print("Retrieving Bids from CURRENT Folders")
    for root, dirs, files in os.walk(root_folder):
        for i in range(len(root)):
            print(f"Progress: {i+1} of {len(root)} directories searched", end="\r")
        if "CURRENT" in dirs:
            current_folder = os.path.join(root, "CURRENT")
            for file in os.listdir(current_folder):
                if file.endswith(".xlsm"):
                    episodes += 1
                    file_paths.append(os.path.join(current_folder, file))
        else:
            for d1 in dirs:
                path1 = os.path.join(root, d1)
                if os.path.isdir(path1):
                    for d2 in os.listdir(path1):
                        path2 = os.path.join(path1, d2)
                        if os.path.isdir(path2):
                            for d3 in os.listdir(path2):
                                path3 = os.path.join(path2, d3)
                                if os.path.isdir(path3) and "CURRENT" in os.listdir(path3):
                                    current_folder = os.path.join(path3, "CURRENT")
                                    for file in os.listdir(current_folder):
                                        if file.endswith(".xlsm"):
                                            episodes += 1
                                            file_paths.append(os.path.join(current_folder, file))
    return episodes, sorted(file_paths)


main()

LMaddalena
  • 31
  • 5
  • 3
    We need to know the code that is throwing the error and ideally on what line it is throwing it on. This is not an error due to not having pandas installed. – Shmack Mar 11 '23 at 19:43
  • Let me know if upgrading numpy doesn't solve your problem. In this case, I will reopen the question. – Corralien Mar 11 '23 at 19:52
  • @Corralien, I did update numpy, this did not fix the issue at hand. – LMaddalena Mar 11 '23 at 19:55
  • @Shmack, I have added my code to the original post. The error happens at line 80, Here is the more verbose error: AttributeError: module 'numpy' has no attribute 'float'. `np.float` was a deprecated alias for the builtin `float`. To avoid this error in existing code, use `float` by itself. Doing this will not modify any behavior and is safe. If you specifically wanted the numpy scalar type, use `np.float64` here. The aliases was originally deprecated in NumPy 1.20; for more details and guidance see the original release note at: – LMaddalena Mar 11 '23 at 19:58
  • I reopened the question but I'm pretty sure the problem is the version of numpy. What is the output of `import numpy as np; print(np.__version__)`? – Corralien Mar 11 '23 at 19:58
  • @Corralien I have imported numpy directly and printed as requested the result was 1.24.2 – LMaddalena Mar 11 '23 at 20:02
  • 1
    Ok so now try `pip install 'numpy<1.24'` to downgrade numpy. Does it work? – Corralien Mar 11 '23 at 20:05
  • @Corralien, The error I needed help with is gone, I got another one but that's something I can track down :) Thank you for all of your help! – LMaddalena Mar 11 '23 at 20:17
  • `Line 80` of what? I examined your posted code, there's no `float` in it. Without a FULL ERROR TRACEBACK we can't even guess what code produced the error. Regarding versions, numpy 1.24 removed `np.float` (following years of warnings). The newest `pandas` should be compatible with that. But if you are using some old pandas, or some other package, then down grading `numpy` is your only fix. – hpaulj Mar 11 '23 at 21:26

0 Answers0