Skip to content

BUG: .at indexing should allow enlargement scalars w/o regards to the type of index (GH8473) #8475

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

Merged
merged 1 commit into from
Oct 5, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
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
13 changes: 9 additions & 4 deletions pandas/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1484,7 +1484,7 @@ class _ScalarAccessIndexer(_NDFrameIndexer):

""" access scalars quickly """

def _convert_key(self, key):
def _convert_key(self, key, is_setter=False):
return list(key)

def __getitem__(self, key):
Expand All @@ -1505,7 +1505,7 @@ def __setitem__(self, key, value):
if len(key) != self.obj.ndim:
raise ValueError('Not enough indexers for scalar access '
'(setting)!')
key = list(self._convert_key(key))
key = list(self._convert_key(key, is_setter=True))
key.append(value)
self.obj.set_value(*key, takeable=self._takeable)

Expand All @@ -1515,8 +1515,13 @@ class _AtIndexer(_ScalarAccessIndexer):
""" label based scalar accessor """
_takeable = False

def _convert_key(self, key):
def _convert_key(self, key, is_setter=False):
""" require they keys to be the same type as the index (so we don't fallback) """

# allow arbitrary setting
if is_setter:
return list(key)

for ax, i in zip(self.obj.axes, key):
if ax.is_integer():
if not com.is_integer(i):
Expand All @@ -1536,7 +1541,7 @@ class _iAtIndexer(_ScalarAccessIndexer):
def _has_valid_setitem_indexer(self, indexer):
self._has_valid_positional_setitem_indexer(indexer)

def _convert_key(self, key):
def _convert_key(self, key, is_setter=False):
""" require integer args (and convert to label arguments) """
for a, i in zip(self.obj.axes, key):
if not com.is_integer(i):
Expand Down
20 changes: 20 additions & 0 deletions pandas/tests/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -2932,6 +2932,26 @@ def f():
p.loc[:,:,'C'] = Series([30,32],index=p_orig.items)
assert_panel_equal(p,expected)

# GH 8473
dates = date_range('1/1/2000', periods=8)
df_orig = DataFrame(np.random.randn(8, 4), index=dates, columns=['A', 'B', 'C', 'D'])

expected = pd.concat([df_orig,DataFrame({'A' : 7},index=[dates[-1]+1])])
df = df_orig.copy()
df.loc[dates[-1]+1, 'A'] = 7
assert_frame_equal(df,expected)
df = df_orig.copy()
df.at[dates[-1]+1, 'A'] = 7
assert_frame_equal(df,expected)

expected = pd.concat([df_orig,DataFrame({0 : 7},index=[dates[-1]+1])],axis=1)
df = df_orig.copy()
df.loc[dates[-1]+1, 0] = 7
assert_frame_equal(df,expected)
df = df_orig.copy()
df.at[dates[-1]+1, 0] = 7
assert_frame_equal(df,expected)

def test_partial_setting_mixed_dtype(self):

# in a mixed dtype environment, try to preserve dtypes
Expand Down