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

BUG: DataFrame.assign accepts 2d arrays for single column value #51925

Open
3 tasks done
determ1ne opened this issue Mar 13, 2023 · 7 comments
Open
3 tasks done

BUG: DataFrame.assign accepts 2d arrays for single column value #51925

determ1ne opened this issue Mar 13, 2023 · 7 comments
Labels
Bug Reshaping Concat, Merge/Join, Stack/Unstack, Explode

Comments

@determ1ne
Copy link

Pandas version checks

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • I have confirmed this bug exists on the main branch of pandas.

Reproducible Example

import sys
import pandas as pd
import numpy as np
from numpy import dtype

print('python version: ', sys.version)
print('pandas version: ', pd.__version__)

data_dtype = [('name', dtype(object)), ('value', 'f8'), ('value2', dtype(object)),]
table = pd.DataFrame(np.zeros(0, dtype=data_dtype), index=pd.Index([], dtype=np.int64))
dtypes = table.dtypes
index = np.arange(10, dtype=np.int32)

data_dict = {'value2': np.ones(10, dtype=float), 'value': np.zeros(10, dtype=float),
             'name': np.array([np.arange(13) + x for x in range(10)], dtype=float)}

dd = pd.DataFrame(index=index, columns=table.columns)
dd = dd.assign(**data_dict)
print('**** Original ****')
print(dd.head())
print('**** Bad Copy ****')
print(dd.copy().head())

print('**** 1D ndarray ****')
data_dict['name'] = data_dict['name'][:, 0]
dd = pd.DataFrame(index=index, columns=table.columns)
dd = dd.assign(**data_dict)
print(dd.copy().head())

'''
Program output:
python version:  3.8.16 | packaged by conda-forge | (default, Feb  1 2023, 15:53:35) [MSC v.1929 64 bit (AMD64)]
pandas version:  0+untagged.1.g6169cba
**** Original ****
   name  value  value2
0   0.0    0.0     1.0
1   1.0    0.0     1.0
2   2.0    0.0     1.0
3   3.0    0.0     1.0
4   4.0    0.0     1.0
**** Bad Copy ****
   name  value  value2
0   0.0    2.0     1.0
1   1.0    3.0     2.0
2   2.0    4.0     3.0
3   3.0    5.0     4.0
4   4.0    6.0     5.0
**** 1D ndarray ****
   name  value  value2
0   0.0    0.0     1.0
1   1.0    0.0     1.0
2   2.0    0.0     1.0
3   3.0    0.0     1.0
4   4.0    0.0     1.0
'''

Issue Description

The name Series was assigned using a 2D ndarray, and it works fine. But a DataFrame.copy makes name overwrite value and value2 in the new df. An 1D ndarray will not break the normal behavior.

Expected Behavior

**** Original ****
   name  value  value2
0   0.0    0.0     1.0
1   1.0    0.0     1.0
2   2.0    0.0     1.0
3   3.0    0.0     1.0
4   4.0    0.0     1.0
**** Bad Copy ****
   name  value  value2
0   0.0    0.0     1.0
1   1.0    0.0     1.0
2   2.0    0.0     1.0
3   3.0    0.0     1.0
4   4.0    0.0     1.0
**** 1D ndarray ****
   name  value  value2
0   0.0    0.0     1.0
1   1.0    0.0     1.0
2   2.0    0.0     1.0
3   3.0    0.0     1.0
4   4.0    0.0     1.0

Installed Versions

Fresh install of development environment following pandas doc gave an error from numba, here's another report:

INSTALLED VERSIONS

commit : 2e218d1
python : 3.8.16.final.0
python-bits : 64
OS : Windows
OS-release : 10
Version : 10.0.22621
machine : AMD64
processor : Intel64 Family 6 Model 158 Stepping 10, GenuineIntel
byteorder : little
LC_ALL : None
LANG : None
LOCALE : Chinese (Simplified)_China.936
pandas : 1.5.3
numpy : 1.22.0
pytz : 2022.7
dateutil : 2.8.2
setuptools : 65.6.3
pip : 22.3.1
Cython : None
pytest : 7.2.2
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : 3.0.9
lxml.etree : None
html5lib : None
pymysql : 1.0.2
psycopg2 : 2.9.5
jinja2 : None
IPython : None
pandas_datareader: None
bs4 : None
bottleneck : 1.3.5
brotli :
fastparquet : None
fsspec : 2023.1.0
gcsfs : None
matplotlib : 3.6.2
numba : 0.56.4
numexpr : 2.8.4
odfpy : None
openpyxl : 3.0.10
pandas_gbq : None
pyarrow : None
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : 1.10.0
snappy : None
sqlalchemy : None
tables : 3.7.0
tabulate : None
xarray : 2023.1.0
xlrd : None
xlwt : None
zstandard : None
tzdata : None

@determ1ne determ1ne added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Mar 13, 2023
@MuhammadNizamani
Copy link

If you do it this way it works fine
print('**** Bad Copy ****') cc = dd.copy().head() print(cc)

@determ1ne
Copy link
Author

If you do it this way it works fine print('**** Bad Copy ****') cc = dd.copy().head() print(cc)

It doesn't work, and the issue seems to be Windows-specific. Are you trying to use macOS or Linux to reproduce this problem?

@PAN-Ziyue
Copy link

Reproducible on my win10 ltsc machine.

@MuhammadNizamani
Copy link

I am using windows 11

@determ1ne
Copy link
Author

I am using windows 11

Could you please try again in a fresh environment from https://github.com/pandas-dev/pandas/blob/main/environment.yml and attach your pd.show_versions() result?

@MuhammadNizamani
Copy link

I checked it today and now it's showing me the same result as yours, there is some bug.

@topper-123
Copy link
Contributor

Thanks for the report, @determ1ne.

I can confirm this problem using MacOS. data_dict["name"] is a 2d array, so the call to assign should def have failed, similarly to it would failed when instantiating a dataframe from the dict:

>>> pd.DataFrame(data_dict)
ValueError: Per-column arrays must each be 1-dimensional

So there is some checks in assign that needs to be tightened. A PR would be appreciated.

@topper-123 topper-123 added Reshaping Concat, Merge/Join, Stack/Unstack, Explode and removed Needs Triage Issue that has not been reviewed by a pandas team member labels May 14, 2023
@topper-123 topper-123 changed the title BUG: Series in a DataFrame can overwrite other Series in copy action BUG: DataFrame.assign accepts 2d arrays for single column value May 14, 2023
determ1ne added a commit to determ1ne/pandas that referenced this issue May 24, 2023
determ1ne added a commit to determ1ne/pandas that referenced this issue May 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Reshaping Concat, Merge/Join, Stack/Unstack, Explode
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants