Skip to content

PERF: Avoid Series constructor in DataFrame(dict(...), columns=) #57205

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 16 commits into from
Feb 24, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
don't overwrite dtype
  • Loading branch information
mroeschke committed Feb 23, 2024
commit 7d81aa26f9d4036d8eadd5996aa1b26969ca8dc1
10 changes: 6 additions & 4 deletions pandas/core/internals/construction.py
Original file line number Diff line number Diff line change
Expand Up @@ -390,11 +390,13 @@ def dict_to_mgr(

# no obvious "empty" int column
if midxs and not is_integer_dtype(dtype):
if dtype is None:
# GH#1783
dtype = np.dtype("object")
# GH#1783
for i in midxs:
arr = construct_1d_arraylike_from_scalar(arrays[i], len(index), dtype)
arr = construct_1d_arraylike_from_scalar(
arrays[i],
len(index),
dtype if dtype is not None else np.dtype("object"),
)
arrays[i] = arr

else:
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/frame/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -3044,7 +3044,7 @@ def test_columns_indexes_raise_on_sets(self):

def test_from_dict_with_columns_na_scalar(self):
result = DataFrame({"a": pd.NaT}, columns=["a"], index=range(2))
expected = DataFrame({"a", Series([pd.NaT, pd.NaT])})
expected = DataFrame({"a": Series([pd.NaT, pd.NaT])})
tm.assert_frame_equal(result, expected)


Expand Down