Fangen einer pyarrow.lib.ArrowInvalid - Exception

Wenn du dir nicht sicher bist, in welchem der anderen Foren du die Frage stellen sollst, dann bist du hier im Forum für allgemeine Fragen sicher richtig.
Antworten
cuddlePanda
User
Beiträge: 9
Registriert: Donnerstag 29. Juli 2021, 17:26

Liebes Forum,

Ich muss mich durch eine größere Datenmenge (als .csv eingelesen) durcharbeiten, um diese dann mittels Pandas in Parquet exportieren zu können.
Bei einer bestimmten .csv - Datei gelingt das auch, bei einer anderen bricht das Programm immer mit einer
pyarrow.lib.ArrowInvalid
Exception ab.
Um das Problem einzugrenzen habe ich bereits einige Spalten aussortiert, bei denen das Problem auftritt, und versucht, auch die Reihen einzugrenzen.
Das Problem dabei: die Datei besteht aus über 400.000 Datensätzen, händisch geht das natürlich nicht mehr.
Ich habe mir daher ein kleines Skript geschrieben, in dem ich in unterschiedlichen Abschnitten eingrenzen kann, wo das Problem auftritt. Dazu muss ich allerdings
die Exception fangen und nicht jedes Mal, wenn der Fehler auftritt, das Programm vom neuen starten zu müssen.

Mein Code - Snipplet für den zentralen Teil sieht so aus:
(column_to_write ist die bereits extrahierte Spalte, row_start und row_end werden in einer Schleife entsprechend der Abschnittsgröße durchlaufen)

this_chunk = column_to_write.iloc[row_start:row_end, 0:1]
try:
this_chunk.to_parquet(out_file_name)
print(" Chunk Nr. " + f"{i: 7d}" + " written successfully to file.")
except pyarrow.lib.ArrowTypeError as a:
print(" Failed for Chunk Nr. " + f"{i: 7d}" +
" (Datasets between " + f"{row_start: 7d}" + " and " + f"{row_end: 7d}" + ").")
print(" thrown message: '" + str(a) + "'.")

Trotz gesetzter Exception stürzt mir das Programm ab, wenn der Fehler auftritt. Was mache ich falsch? Vielleicht kann mir ja jemand einen Tipp geben.

Vielen Dank
Sirius3
User
Beiträge: 17710
Registriert: Sonntag 21. Oktober 2012, 17:20

Du benutzt f-Strings falsch. Denn die sind ja gerade dazu da, dass man nicht Strings per + zusammenstückeln muß.

Code: Alles auswählen

    this_chunk = column_to_write.iloc[row_start:row_end, 0:1]
    try:
        this_chunk.to_parquet(out_file_name)
        print(f" Chunk Nr. {i:7d} written successfully to file.")
    except pyarrow.lib.ArrowTypeError as a:
        print(f" Failed for Chunk Nr. {i:7d}"
              f" (Datasets between {row_start:7d} and {row_end: 7d}).")
        print(f"        thrown message: {a}.")
Und bitte neben dem relevanten Code auch den komplette Traceback mitschicken; so kann man Dir nicht helfen.
cuddlePanda
User
Beiträge: 9
Registriert: Donnerstag 29. Juli 2021, 17:26

Vielen Dank einmal für die erste Antwort.

Mein aktueller Traceback sieht so aus:

Traceback (most recent call last):
File "/home/martin/PycharmProjects/copy/pythonProject5/direct_leads.py", line 86, in <module>
this_chunk.to_parquet(out_file_name)
File "/home/martin/PycharmProjects/pythonProject5/venv/lib/python3.8/site-packages/pandas/util/_decorators.py", line 207, in wrapper
return func(*args, **kwargs)
File "/home/martin/PycharmProjects/pythonProject5/venv/lib/python3.8/site-packages/pandas/core/frame.py", line 2677, in to_parquet
return to_parquet(
File "/home/martin/PycharmProjects/pythonProject5/venv/lib/python3.8/site-packages/pandas/io/parquet.py", line 412, in to_parquet
impl.write(
File "/home/martin/PycharmProjects/pythonProject5/venv/lib/python3.8/site-packages/pandas/io/parquet.py", line 173, in write
table = self.api.Table.from_pandas(df, **from_pandas_kwargs)
File "pyarrow/table.pxi", line 1553, in pyarrow.lib.Table.from_pandas
File "/home/martin/PycharmProjects/pythonProject5/venv/lib/python3.8/site-packages/pyarrow/pandas_compat.py", line 594, in dataframe_to_arrays
arrays = [convert_column(c, f)
File "/home/martin/PycharmProjects/pythonProject5/venv/lib/python3.8/site-packages/pyarrow/pandas_compat.py", line 594, in <listcomp>
arrays = [convert_column(c, f)
File "/home/martin/PycharmProjects/pythonProject5/venv/lib/python3.8/site-packages/pyarrow/pandas_compat.py", line 581, in convert_column
raise e
File "/home/martin/PycharmProjects/pythonProject5/venv/lib/python3.8/site-packages/pyarrow/pandas_compat.py", line 575, in convert_column
result = pa.array(col, type=type_, from_pandas=True, safe=safe)
File "pyarrow/array.pxi", line 302, in pyarrow.lib.array
File "pyarrow/array.pxi", line 83, in pyarrow.lib._ndarray_to_array
File "pyarrow/error.pxi", line 97, in pyarrow.lib.check_status
pyarrow.lib.ArrowInvalid: ("Could not convert '48713444551' with type str: tried to convert to double", 'Conversion failed for column mobile with type object')

Process finished with exit code 1


(ich habe soeben festgestellt, dass in einem früheren Traceback (siehe unten) in der letzten Zeile: pyarrow.lib.ArrowTypeError gestanden ist, das habe ich
dann in die except - clause eingefügt gehabt. Jetzt habe ich beides drin...
außerdem habe ich die f-Strings einmal rausgeschmissen (die Formatierung wäre in diesem Zusammenhang ohnehin eher Luxus...) und jetzt funktioniert's.

Wie ich die f-Strings richtig anwende (habe ich mir erst vor kurzem angesehen und offenbar noch nicht genau überzogen...) werde ich mir noch einmal genau
ansehen. Ich werde einmal nachvollziehen, aus welchem Manual/Tutorial ich mir das ganze mit den f-Strings herausgezogen habe, vielleicht finde ich noch
etwas besseres dazu.
Auf jeden Fall einmal vielen Dank nochmals.


PS.: Hier noch der Traceback in der ursprünglichen Version (bei dem ich dann auch die ArrowTypeError gekommen bin...:
Traceback (most recent call last):
File "/home/martin/PycharmProjects/copy/pythonProject5/direct_leads.py", line 33, in <module>
column_to_write.to_parquet(out_file_name)
File "/home/martin/PycharmProjects/pythonProject5/venv/lib/python3.8/site-packages/pandas/util/_decorators.py", line 207, in wrapper
return func(*args, **kwargs)
File "/home/martin/PycharmProjects/pythonProject5/venv/lib/python3.8/site-packages/pandas/core/frame.py", line 2677, in to_parquet
return to_parquet(
File "/home/martin/PycharmProjects/pythonProject5/venv/lib/python3.8/site-packages/pandas/io/parquet.py", line 412, in to_parquet
impl.write(
File "/home/martin/PycharmProjects/pythonProject5/venv/lib/python3.8/site-packages/pandas/io/parquet.py", line 173, in write
table = self.api.Table.from_pandas(df, **from_pandas_kwargs)
File "pyarrow/table.pxi", line 1553, in pyarrow.lib.Table.from_pandas
File "/home/martin/PycharmProjects/pythonProject5/venv/lib/python3.8/site-packages/pyarrow/pandas_compat.py", line 594, in dataframe_to_arrays
arrays = [convert_column(c, f)
File "/home/martin/PycharmProjects/pythonProject5/venv/lib/python3.8/site-packages/pyarrow/pandas_compat.py", line 594, in <listcomp>
arrays = [convert_column(c, f)
File "/home/martin/PycharmProjects/pythonProject5/venv/lib/python3.8/site-packages/pyarrow/pandas_compat.py", line 581, in convert_column
raise e
File "/home/martin/PycharmProjects/pythonProject5/venv/lib/python3.8/site-packages/pyarrow/pandas_compat.py", line 575, in convert_column
result = pa.array(col, type=type_, from_pandas=True, safe=safe)
File "pyarrow/array.pxi", line 302, in pyarrow.lib.array
File "pyarrow/array.pxi", line 83, in pyarrow.lib._ndarray_to_array
File "pyarrow/error.pxi", line 120, in pyarrow.lib.check_status
pyarrow.lib.ArrowTypeError: ("Expected bytes, got a 'float' object", 'Conversion failed for column mobile with type object')

Process finished with exit code 1
Antworten