Skip to content

BUG: 1.2. version fails in adding column if number on column name beginning. #39010

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

Closed
3 tasks done
Malachov opened this issue Jan 6, 2021 · 3 comments · Fixed by #39023
Closed
3 tasks done

BUG: 1.2. version fails in adding column if number on column name beginning. #39010

Malachov opened this issue Jan 6, 2021 · 3 comments · Fixed by #39023
Labels
Bug Indexing Related to indexing on series/frames, not to indexes themselves Regression Functionality that used to work in a prior pandas version
Milestone

Comments

@Malachov
Copy link

Malachov commented Jan 6, 2021

  • I have checked that this issue has not already been reported.
  • I have confirmed this bug exists on the latest version of pandas.
  • (optional) I have confirmed this bug exists on the master branch of pandas.

Code that raise traceback

import pandas as pd

df = pd.DataFrame([[1, 2], [3, 4]])
df['0 - Name'] = [1, 2]

Result:

ValueError: format number 1 of "0 - Name" is not recognized

If other name witho no mumber on column name, it will pass.
In older version, worked correctly

Tested on new virtualenv - just pip install pandas
Failed also on Travis (Python 3.6 passed, 3.7 and 3.8 failed), so probably not my IDE setup or something similar

Full traceback here...

Traceback (most recent call last):
  File "c:\Users\truton\ownCloud\Github\test\del\lib\site-packages\pandas\core\generic.py", line 3823, in _set_item
    loc = self._info_axis.get_loc(key)
  File "c:\Users\truton\ownCloud\Github\test\del\lib\site-packages\pandas\core\indexes\range.py", line 354, in get_loc
    raise KeyError(key)
KeyError: '0 - Name'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "c:\Users\truton\ownCloud\Github\test\del.py", line 4, in <module>
    df['0 - Name'] = [1, 2]
  File "c:\Users\truton\ownCloud\Github\test\del\lib\site-packages\pandas\core\frame.py", line 3163, in __setitem__
    self._set_item(key, value)
  File "c:\Users\truton\ownCloud\Github\test\del\lib\site-packages\pandas\core\frame.py", line 3240, in _set_item
    NDFrame._set_item(self, key, value)
  File "c:\Users\truton\ownCloud\Github\test\del\lib\site-packages\pandas\core\generic.py", line 3826, in _set_item
    self._mgr.insert(len(self._info_axis), key, value)
  File "c:\Users\truton\ownCloud\Github\test\del\lib\site-packages\pandas\core\internals\managers.py", line 1197,
in insert
    new_axis = self.items.insert(loc, item)
  File "c:\Users\truton\ownCloud\Github\test\del\lib\site-packages\pandas\core\indexes\numeric.py", line 174, in insert
    item = self._validate_fill_value(item)
  File "c:\Users\truton\ownCloud\Github\test\del\lib\site-packages\pandas\core\indexes\numeric.py", line 119, in _validate_fill_value
    if is_bool(value) or is_bool_dtype(value):
  File "c:\Users\truton\ownCloud\Github\test\del\lib\site-packages\pandas\core\dtypes\common.py", line 1384, in is_bool_dtype
    dtype = get_dtype(arr_or_dtype)
  File "c:\Users\truton\ownCloud\Github\test\del\lib\site-packages\pandas\core\dtypes\common.py", line 1607, in get_dtype
    return pandas_dtype(arr_or_dtype)
  File "c:\Users\truton\ownCloud\Github\test\del\lib\site-packages\pandas\core\dtypes\common.py", line 1799, in pandas_dtype
    npdtype = np.dtype(dtype)
  File "c:\Users\truton\ownCloud\Github\test\del\lib\site-packages\numpy\core\_internal.py", line 177, in _commastring
    (len(result)+1, astr))
ValueError: format number 1 of "0 - Name" is not recognized

Problem description

Very strange is than it happens only if it's first assignment, so this will work correctly with no error

import pandas as pd

df = pd.DataFrame([[1, 2], [3, 4]])
df['a - Name'] = [1, 2]
df['0 - Name'] = [1, 2]

Expected Output

I do not create dataframe with numeric names, but thing is, that if source data are numpy, than created dataframe has numeric range index. If user in my library insert data - i create some derivations - want to keep names + name of derivation - thus if somebody insert numpy, it will fail.

Expected output is creating new column with number in column name start - or generating columns like ['1', '2', '3'] from arrays which works...

Output of pd.show_versions()

INSTALLED VERSIONS

commit : 3e89b4c
python : 3.7.7.final.0
python-bits : 64
OS : Windows
OS-release : 10
Version : 10.0.17763
machine : AMD64
processor : Intel64 Family 6 Model 94 Stepping 3, GenuineIntel
byteorder : little
LC_ALL : None
LANG : cs_CZ.UTF-8
LOCALE : None.None

pandas : 1.2.0
numpy : 1.19.5
pytz : 2020.5
dateutil : 2.8.1
pip : 20.1
setuptools : 46.1.3
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : None
IPython : None
pandas_datareader: None
bs4 : None
bottleneck : None
fsspec : None
fastparquet : None
gcsfs : None
matplotlib : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
numba : None

@Malachov Malachov added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Jan 6, 2021
@phofl
Copy link
Member

phofl commented Jan 7, 2021

Hi, thanks for your report. This is an interesting edge case. With your raising example we have a RangeIndex, so we dispatch to numy to check for bool dtype

np.dtype("0 - name")

raises

Traceback (most recent call last):
  File "/home/developer/.config/JetBrains/PyCharm2020.3/scratches/scratch_4.py", line 407, in <module>
    np.dtype('0 - Name')
  File "/home/developer/anaconda3/envs/omi_reports/pandas-dev/lib/python3.8/site-packages/numpy/core/_internal.py", line 175, in _commastring
    raise ValueError(
ValueError: format number 1 of "0 - Name" is not recognized

In your second example we have an Index with dtype object, where we do things a litte bit different,

@phofl phofl added Indexing Related to indexing on series/frames, not to indexes themselves Regression Functionality that used to work in a prior pandas version and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Jan 7, 2021
@phofl phofl added this to the 1.2.1 milestone Jan 7, 2021
@phofl
Copy link
Member

phofl commented Jan 7, 2021

Marking as a Regression since this worked on 1.1.5.

@asishm
Copy link
Contributor

asishm commented Jan 7, 2021

d0afefa is the first bad commit
commit d0afefa
Author: jbrockmendel [email protected]
Date: Thu Nov 26 09:25:59 2020 -0800

BUG: NumericIndex.insert(0, False) casting to int (#38030)

#38030
cc @jbrockmendel

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Indexing Related to indexing on series/frames, not to indexes themselves Regression Functionality that used to work in a prior pandas version
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants