Unterstützt Polare die Erstellung eines Datenrahmens aus einem verschachtelten Wörterbuch?
Posted: 07 Feb 2025, 03:33
Ich versuche, einen Polars -Datenframe aus einem Wörterbuch (MainDict) zu erstellen, in dem einer der Werte des Hauptdikts eine Liste von Diktatobjekten (NestedDicts) ist. Wenn ich dies versuche, bekomme ich einen Fehler (siehe unten), den ich nicht kenne. Pandas erlaubt mir jedoch, einen Datenrahmen mit dem MainDict zu erstellen. < /P>
Ich bin mir nicht sicher, ob ich etwas falsch mache, wenn es sich unterstützt von Polaren. Ich mache mir keine große Sorgen darüber, eine Problemumgehung zu finden, da es unkompliziert sein sollte (Vorschläge sind willkommen), aber ich würde es gerne so tun, wenn möglich. < /P>
Ich bin auf Polar Version 0.13.38 auf Google Colab (Problem findet auch lokal auf VSCODE mit Python Version 3.9.6 und Windows 10 statt). Im Folgenden finden Sie ein Beispiel für Code, das das Problem und seine Ausgabe reproduziert. Danke! < /P>
Eingabe: < /p>
Ich bin mir nicht sicher, ob ich etwas falsch mache, wenn es sich unterstützt von Polaren. Ich mache mir keine große Sorgen darüber, eine Problemumgehung zu finden, da es unkompliziert sein sollte (Vorschläge sind willkommen), aber ich würde es gerne so tun, wenn möglich. < /P>
Ich bin auf Polar Version 0.13.38 auf Google Colab (Problem findet auch lokal auf VSCODE mit Python Version 3.9.6 und Windows 10 statt). Im Folgenden finden Sie ein Beispiel für Code, das das Problem und seine Ausgabe reproduziert. Danke! < /P>
Eingabe: < /p>
Code: Select all
import polars as pl
import pandas as pd
template = { 'a':['A', 'AA'],
'b':['B', 'BB'],
'c':['C', 'CC'],
'd':[{'D1':'D2'}, {'DD1':'DD2'}]}
#create a dataframe using pandas
df_pandas = pd.DataFrame(template)
print(df_pandas)
#create a dataframe using polars
df_polars = pl.DataFrame(template)
print(df_polars)
< /code>
Ausgabe: < /p>
a b c d
0 A B C {'D1': 'D2'}
1 AA BB CC {'DD1': 'DD2'}
---------------------------------------------------------------------------
ComputeError Traceback (most recent call last)
in ()
12
13 #create a dataframe using polars
---> 14 df_polars = pl.DataFrame(template)
15 print(df_polars)
3 frames
/usr/local/lib/python3.7/dist-packages/polars/internals/frame.py in __init__(self, data, columns, orient)
300
301 elif isinstance(data, dict):
--> 302 self._df = dict_to_pydf(data, columns=columns)
303
304 elif isinstance(data, np.ndarray):
/usr/local/lib/python3.7/dist-packages/polars/internals/construction.py in dict_to_pydf(data, columns)
400 return PyDataFrame(data_series)
401 # fast path
--> 402 return PyDataFrame.read_dict(data)
403
404
/usr/local/lib/python3.7/dist-packages/polars/internals/series.py in __init__(self, name, values, dtype, strict, nan_to_null)
225 self._s = self.cast(dtype, strict=True)._s
226 elif isinstance(values, Sequence):
--> 227 self._s = sequence_to_pyseries(name, values, dtype=dtype, strict=strict)
228 elif _PANDAS_AVAILABLE and isinstance(values, (pd.Series, pd.DatetimeIndex)):
229 self._s = pandas_to_pyseries(name, values)
/usr/local/lib/python3.7/dist-packages/polars/internals/construction.py in sequence_to_pyseries(name, values, dtype, strict)
241 if constructor == PySeries.new_object:
242 try:
--> 243 return PySeries.new_from_anyvalues(name, values)
244 # raised if we cannot convert to Wrap
245 except RuntimeError:
ComputeError: struct orders must remain the same