Skip to content
New issue

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

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

Already on GitHub? # to your account

DEP: Deprecate passing a DataFrame to from_records #51697

Merged
merged 8 commits into from
Mar 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v2.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ Other API changes
Deprecations
~~~~~~~~~~~~
- Deprecating pinning ``group.name`` to each group in :meth:`SeriesGroupBy.aggregate` aggregations; if your operation requires utilizing the groupby keys, iterate over the groupby object instead (:issue:`41090`)
- Deprecated passing a :class:`DataFrame` to :meth:`DataFrame.from_records`, use :meth:`DataFrame.set_index` or :meth:`DataFrame.drop` instead (:issue:`51353`)
- Deprecated accepting slices in :meth:`DataFrame.take`, call ``obj[slicer]`` or pass a sequence of integers instead (:issue:`51539`)
-

Expand Down
22 changes: 21 additions & 1 deletion pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -2109,6 +2109,9 @@ def from_records(
----------
data : structured ndarray, sequence of tuples or dicts, or DataFrame
Structured input data.

.. deprecated:: 2.1.0
Passing a DataFrame is deprecated.
index : str, list of fields, array-like
Field of array to use as the index, alternately a specific set of
input labels to use.
Expand Down Expand Up @@ -2171,6 +2174,23 @@ def from_records(
2 1 c
3 0 d
"""
if isinstance(data, DataFrame):
warnings.warn(
"Passing a DataFrame to DataFrame.from_records is deprecated. Use "
"set_index and/or drop to modify the DataFrame instead.",
FutureWarning,
stacklevel=find_stack_level(),
)
if columns is not None:
if is_scalar(columns):
columns = [columns]
data = data[columns]
if index is not None:
data = data.set_index(index)
if exclude is not None:
data = data.drop(columns=exclude)
return data

result_index = None

# Make a copy of the input columns so we can modify it
Expand Down Expand Up @@ -2238,7 +2258,7 @@ def maybe_reorder(
arrays, arr_columns, columns, index
)

elif isinstance(data, (np.ndarray, DataFrame)):
elif isinstance(data, np.ndarray):
arrays, columns = to_arrays(data, columns)
arr_columns = columns
else:
Expand Down
13 changes: 0 additions & 13 deletions pandas/core/internals/construction.py
Original file line number Diff line number Diff line change
Expand Up @@ -769,19 +769,6 @@ def to_arrays(
-----
Ensures that len(result_arrays) == len(result_index).
"""
if isinstance(data, ABCDataFrame):
# see test_from_records_with_index_data, test_from_records_bad_index_column
if columns is not None:
arrays = [
data._ixs(i, axis=1)._values
for i, col in enumerate(data.columns)
if col in columns
]
else:
columns = data.columns
arrays = [data._ixs(i, axis=1)._values for i in range(len(columns))]

return arrays, columns

if not len(data):
if isinstance(data, np.ndarray):
Expand Down
24 changes: 15 additions & 9 deletions pandas/tests/frame/constructors/test_from_records.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ def test_from_records_dt64tz_frame(self):
# GH#51162 don't lose tz when calling from_records with DataFrame input
dti = date_range("2016-01-01", periods=10, tz="US/Pacific")
df = DataFrame({i: dti for i in range(4)})
res = DataFrame.from_records(df)
with tm.assert_produces_warning(FutureWarning):
res = DataFrame.from_records(df)
tm.assert_frame_equal(res, df)

def test_from_records_with_datetimes(self):
Expand Down Expand Up @@ -177,29 +178,34 @@ def test_from_records_with_index_data(self):
df = DataFrame(np.random.randn(10, 3), columns=["A", "B", "C"])

data = np.random.randn(10)
df1 = DataFrame.from_records(df, index=data)
with tm.assert_produces_warning(FutureWarning):
df1 = DataFrame.from_records(df, index=data)
tm.assert_index_equal(df1.index, Index(data))

def test_from_records_bad_index_column(self):
df = DataFrame(np.random.randn(10, 3), columns=["A", "B", "C"])

# should pass
df1 = DataFrame.from_records(df, index=["C"])
with tm.assert_produces_warning(FutureWarning):
df1 = DataFrame.from_records(df, index=["C"])
tm.assert_index_equal(df1.index, Index(df.C))

df1 = DataFrame.from_records(df, index="C")
with tm.assert_produces_warning(FutureWarning):
df1 = DataFrame.from_records(df, index="C")
tm.assert_index_equal(df1.index, Index(df.C))

# should fail
msg = "|".join(
[
r"Length of values \(10\) does not match length of index \(1\)",
r"'None of \[2\] are in the columns'",
]
)
with pytest.raises(ValueError, match=msg):
DataFrame.from_records(df, index=[2])
with pytest.raises(KeyError, match=r"^2$"):
DataFrame.from_records(df, index=2)
with pytest.raises(KeyError, match=msg):
with tm.assert_produces_warning(FutureWarning):
DataFrame.from_records(df, index=[2])
with pytest.raises(KeyError, match=msg):
with tm.assert_produces_warning(FutureWarning):
DataFrame.from_records(df, index=2)

def test_from_records_non_tuple(self):
class Record:
Expand Down