Skip to content
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

[python-package] make a shallow copy on dataframe rename (fixes #4596) #5254

Merged
merged 2 commits into from
Jun 5, 2022
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
2 changes: 1 addition & 1 deletion python-package/lightgbm/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,7 @@ def _data_from_pandas(data, feature_name, categorical_feature, pandas_categorica
if len(data.shape) != 2 or data.shape[0] < 1:
raise ValueError('Input data must be 2 dimensional and non empty.')
if feature_name == 'auto' or feature_name is None:
data = data.rename(columns=str)
data = data.rename(columns=str, copy=False)
cat_cols = [col for col, dtype in zip(data.columns, data.dtypes) if isinstance(dtype, pd_CategoricalDtype)]
cat_cols_not_ordered = [col for col in cat_cols if not data[col].cat.ordered]
if pandas_categorical is None: # train dataset
Expand Down
6 changes: 2 additions & 4 deletions tests/python_package_test/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -648,9 +648,7 @@ def test_no_copy_when_single_float_dtype_dataframe(dtype):
pd = pytest.importorskip('pandas')
X = np.random.rand(10, 2).astype(dtype)
df = pd.DataFrame(X)
# feature names are required to not make a copy (rename makes a copy)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test currently tests "when you pass a pandas DataFrame and explicit list of feature names, a copy is not made".

This PR is proposing changing it to "when you pass a pandas DataFrame and feature_names="auto", a copy is not made".

To test the broader behavior "when you pass a pandas DataFrame, a copy is not made, regardless of how you specify feature names", would you consider instead changing the tests touched in this PR such that both cases (feature_name and "auto") are tested?

feature_name = ['x1', 'x2']
built_data = lgb.basic._data_from_pandas(df, feature_name, None, None)[0]
built_data = lgb.basic._data_from_pandas(df, 'auto', None, None)[0]
assert built_data.dtype == dtype
assert np.shares_memory(X, built_data)

Expand All @@ -659,7 +657,7 @@ def test_categorical_code_conversion_doesnt_modify_original_data():
pd = pytest.importorskip('pandas')
X = np.random.choice(['a', 'b'], 100).reshape(-1, 1)
df = pd.DataFrame(X.copy(), columns=['x1'], dtype='category')
data = lgb.basic._data_from_pandas(df, ['x1'], None, None)[0]
data = lgb.basic._data_from_pandas(df, 'auto', None, None)[0]
# check that the original data wasn't modified
np.testing.assert_equal(df['x1'], X[:, 0])
# check that the built data has the codes
Expand Down