0

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: data in the initial file

Final result: final Result

loveALgo
  • 13
  • 4
  • Use `iloc` instead `ix` – jezrael Apr 21 '20 at 11:11
  • 1
    i did it, i have this error "TypeError: [datetime.datetime(2017, 1, 1, 0, 0, tzinfo=tzoffset(None, 3600)) datetime.datetime(2017, 1, 1, 0, 10, tzinfo=tzoffset(None, 3600))...." – loveALgo Apr 21 '20 at 11:17
  • Problem is with `data['dt'] = pd.DatetimeIndex(data.ix[:, 0])` converted to `data['dt'] = pd.DatetimeIndex(data.iloc[:, 0])` ? – jezrael Apr 21 '20 at 11:21
  • yes! Traceback (most recent call last): File "c:/Users/SMSARR/Desktop/Codes python capitole/Reshaper/reshaper.py", line 36, in data['dt'] = pd.DatetimeIndex(data.iloc[:, 0]) ... raise TypeError(result) TypeError: [datetime.datetime(2017, 1, 1, 0, 0, tzinfo=tzoffset(None, 3600)) datetime.datetime(2017, 1, 1, 0, 10, tzinfo=tzoffset(None, 3600))... datetime.datetime(2017, 12, 31, 23, 40, tzinfo=tzoffset(None, 3600)) datetime.datetime(2017, 12, 31, 23, 50, tzinfo=tzoffset(None, 3600))] – loveALgo Apr 21 '20 at 11:29
  • One idea, how working `data['dt'] = pd.to_datetime(data.iloc[:, 0])` ? – jezrael Apr 21 '20 at 11:34
  • Traceback (most recent call last): File "c:/Users/SMSARR/Desktop/Codes python capitole/Reshaper/reshaper.py", line 37, in data = data.set_index(data['dt']).tz_localize("UTC").tz_convert("Europe/Paris") ... raise TypeError( TypeError: index is not a valid DatetimeIndex or PeriodIndex – loveALgo Apr 21 '20 at 11:40
  • Not idea what is problem, so reopened. – jezrael Apr 21 '20 at 11:57
  • when i change ix to iloc here: monthed = df.ix[df["month of year"] == month] to HPE_monthed = df.iloc[df["month of year"] == month] i have thi error: ValueError: iLocation based boolean indexing cannot use an indexable as a mask. – loveALgo Apr 22 '20 at 17:16

1 Answers1

0

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()
loveALgo
  • 13
  • 4