简体   繁体   中英

transform pandas.Dataframe.ix in the new version of pandas

Hello I am entered to update a code that used pandas 0.23.4 the line data ['dt'] = pd.DatetimeIndex (data.ix [:, 0]) brings me errors after upgrading pandas and python. According to research, this function (pandas.dataframe.ix) has been removed. Which method can be used to replace it? Basically what the code does, it does mean values per hour, which it writes to a new folder

import pandas as pd
from msvcrt import getch
from os import listdir

#reading file and user input
file_name = [filename for filename in listdir() if (".csv" or ".txt") in filename]

if not file_name:
    print("\nThere are no .csv files in folder")
    print(listdir())
else:
    for file in file_name:

        print("\nParsing {}".format(file))

        typ = file[-4:] #get extension of file

        if (typ == ".csv"):
            data = pd.read_csv(file, sep=';', encoding='utf-8', skiprows = 2)
            #subcase for different type of separators
            if len(data.columns) == 1:
                data = pd.read_csv(file, sep=',', encoding='utf-8', skiprows = 2)

        else:
            data = pd.read_csv(file, sep=';', encoding='utf-16', skiprows = 2)
            if len(data.columns) == 1:
                data = pd.read_csv(file, sep=',', encoding='utf-16', skiprows = 2)


        data['Valeur'] = data['Valeur'] / 1000
        data['dt'] = pd.DatetimeIndex(data.ix[:, 0])
        data = data.set_index(data['dt']).tz_localize("UTC").tz_convert("Europe/Paris")
        data['Valeur'].resample('1h').mean().to_csv("output//{}_0.csv".format(file[:-4].split(".")[0]))

print("\nPress any key (other than ALT) to exit")
getch()

data in the initial file: 初始文件中的数据

Final result: 最后结果

i found the solution

import pandas as pd
from msvcrt import getch
from os import listdir

#reading file and user input
file_name = [filename for filename in listdir() if (".csv" or ".txt") in filename]

if not file_name:
    print("\nThere are no .csv files in folder")
    print(listdir())
else:
    for file in file_name:

        print("\nParsing {}".format(file))

        typ = file[-4:] #get extension of file

        if (typ == ".csv"):
            data = pd.read_csv(file, sep=';', encoding='utf-8', skiprows = 2)
            #subcase for different type of separators
            if len(data.columns) == 1:
                data = pd.read_csv(file, sep=',', encoding='utf-8', skiprows = 2)

        else:
            data = pd.read_csv(file, sep=';', encoding='utf-16', skiprows = 2)
            if len(data.columns) == 1:
                data = pd.read_csv(file, sep=',', encoding='utf-16', skiprows = 2)


        data['Valeur'] = data['Valeur'] / 1000

        data['dt'] = pd.to_datetime(data.iloc[:, 0],utc=True)

        data = data.set_index(pd.DatetimeIndex(data['dt'])).tz_convert("Europe/Paris")



        data['Valeur'].resample('1h').mean().to_csv("output//{}_0.csv".format(file[:-4].split(".")[0]),header=False)
        print("fin de Reshaper, ciao!")

print("\nPress any key (other than ALT) to exit")
getch()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM