Skip to content

Commit

Permalink
Add support for nested dict in DataFrame constructor (#14119)
Browse files Browse the repository at this point in the history
Fixes: #14096 

This PR enables nested dict initialization support in `DataFrame` constructor.

Authors:
  - GALI PREM SAGAR (https://github.com/galipremsagar)

Approvers:
  - Matthew Roeschke (https://github.com/mroeschke)

URL: #14119
  • Loading branch information
galipremsagar authored Sep 18, 2023
1 parent 5935ef3 commit 8e081c0
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
4 changes: 2 additions & 2 deletions python/cudf/cudf/core/dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -977,7 +977,7 @@ def _align_input_series_indices(data, index):
input_series = [
Series(val)
for val in data.values()
if isinstance(val, (pd.Series, Series))
if isinstance(val, (pd.Series, Series, dict))
]

if input_series:
Expand All @@ -994,7 +994,7 @@ def _align_input_series_indices(data, index):
index = aligned_input_series[0].index

for name, val in data.items():
if isinstance(val, (pd.Series, Series)):
if isinstance(val, (pd.Series, Series, dict)):
data[name] = aligned_input_series.pop(0)

return data, index
Expand Down
19 changes: 19 additions & 0 deletions python/cudf/cudf/tests/test_dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -10349,3 +10349,22 @@ def test_dataframe_round_builtin(digits):
actual = round(gdf, digits)

assert_eq(expected, actual)


def test_dataframe_init_from_nested_dict():
ordered_dict = OrderedDict(
[
("one", OrderedDict([("col_a", "foo1"), ("col_b", "bar1")])),
("two", OrderedDict([("col_a", "foo2"), ("col_b", "bar2")])),
("three", OrderedDict([("col_a", "foo3"), ("col_b", "bar3")])),
]
)
pdf = pd.DataFrame(ordered_dict)
gdf = cudf.DataFrame(ordered_dict)

assert_eq(pdf, gdf)
regular_dict = {key: dict(value) for key, value in ordered_dict.items()}

pdf = pd.DataFrame(regular_dict)
gdf = cudf.DataFrame(regular_dict)
assert_eq(pdf, gdf)

0 comments on commit 8e081c0

Please sign in to comment.