BUG: 1.2. version fails in adding column if number on column name beginning. · Issue #39010 · pandas-dev/pandas (original) (raw)


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 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