Skip to content

Updating value of a single row of a column using loc or at fails #22040

Open
@abhinav-upadhyay

Description

@abhinav-upadhyay

Code Sample, a copy-pastable example if possible

In [12]: import numpy as np

In [13]: import pandas as pd

In [14]: arr = np.random.rand(2, 2)

In [15]: colnames = ['col1', 'col2']

In [16]: index = pd.date_range('1-1-2018', periods=2)

In [17]: df = pd.DataFrame(data=arr, index=index, columns=colnames)

In [18]: df
Out[18]:
                col1      col2
2018-01-01  0.395883  0.291811
2018-01-02  0.019188  0.302100

In [19]: df.loc[0, 'col1'] = 0
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
~/venvs/bokeh/lib/python3.6/site-packages/pandas/core/indexes/datetimes.py in insert(self, loc, item)
   2182             import pdb; pdb.set_trace()
-> 2183             new_dates = np.concatenate((self[:loc].asi8, [item.view(np.int64)],
   2184                                         self[loc:].asi8))

AttributeError: 'int' object has no attribute 'view'

During handling of the above exception, another exception occurred:

TypeError                                 Traceback (most recent call last)
<ipython-input-19-a2498475711c> in <module>()
----> 1 df.loc[0, 'col1'] = 0

~/venvs/bokeh/lib/python3.6/site-packages/pandas/core/indexing.py in __setitem__(self, key, value)
    187             key = com._apply_if_callable(key, self.obj)
    188         indexer = self._get_setitem_indexer(key)
--> 189         self._setitem_with_indexer(indexer, value)
    190
    191     def _validate_key(self, key, axis):

~/venvs/bokeh/lib/python3.6/site-packages/pandas/core/indexing.py in _setitem_with_indexer(self, indexer, value)
    374                     # so the object is the same
    375                     index = self.obj._get_axis(i)
--> 376                     labels = index.insert(len(index), key)
    377                     self.obj._data = self.obj.reindex(labels, axis=i)._data
    378                     self.obj._maybe_update_cacher(clear=True)

~/venvs/bokeh/lib/python3.6/site-packages/pandas/core/indexes/datetimes.py in insert(self, loc, item)
   2181         try:
   2182             import pdb; pdb.set_trace()
-> 2183             new_dates = np.concatenate((self[:loc].asi8, [item.view(np.int64)],
   2184                                         self[loc:].asi8))
   2185             if self.tz is not None:

TypeError: cannot insert DatetimeIndex with incompatible label

Problem description

Trying to update the value of a single row of a column in a dataframe with DatetimeIndex using .loc or .at leads to an error. For instance

df.loc[0, 'col1'] = 0

fails, while

df.loc[0:1, 'col1'] = 0

works.

Expected Output

Expected to update the value of first col1 in first row to be set to 0

                col1      col2
2018-01-01  0.000000  0.291811
2018-01-02  0.019188  0.302100

Output of pd.show_versions()

[paste the output of pd.show_versions() here below this line]
In [20]: pd.show_versions()

INSTALLED VERSIONS

commit: None
python: 3.6.5.final.0
python-bits: 64
OS: Linux
OS-release: 4.15.0-24-generic
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: en_IN
LOCALE: en_IN.ISO8859-1

pandas: 0.23.3
pytest: None
pip: 10.0.1
setuptools: 40.0.0
Cython: None
numpy: 1.14.5
scipy: 1.1.0
pyarrow: None
xarray: None
IPython: 6.4.0
sphinx: None
patsy: None
dateutil: 2.7.3
pytz: 2018.5
blosc: None
bottleneck: None
tables: None
numexpr: None
feather: None
matplotlib: 2.2.2
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml: None
bs4: None
html5lib: 1.0.1
sqlalchemy: None
pymysql: None
psycopg2: None
jinja2: 2.10
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions