-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
BUG: .iloc and .loc behavior not consistent on empty dataframe #9983
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1063,6 +1063,7 @@ def test_loc_setitem_consistency(self): | |
|
||
# empty (essentially noops) | ||
expected = DataFrame(columns=['x', 'y']) | ||
expected['x'] = expected['x'].astype(np.int64) | ||
df = DataFrame(columns=['x', 'y']) | ||
df.loc[:, 'x'] = 1 | ||
assert_frame_equal(df,expected) | ||
|
@@ -3376,7 +3377,7 @@ def f(): | |
expected = DataFrame(columns=['foo']) | ||
def f(): | ||
df = DataFrame() | ||
df['foo'] = Series([]) | ||
df['foo'] = Series([], dtype='object') | ||
return df | ||
assert_frame_equal(f(), expected) | ||
def f(): | ||
|
@@ -3386,17 +3387,20 @@ def f(): | |
assert_frame_equal(f(), expected) | ||
def f(): | ||
df = DataFrame() | ||
df['foo'] = Series(range(len(df))) | ||
df['foo'] = df.index | ||
return df | ||
assert_frame_equal(f(), expected) | ||
|
||
expected = DataFrame(columns=['foo']) | ||
expected['foo'] = expected['foo'].astype('float64') | ||
def f(): | ||
df = DataFrame() | ||
df['foo'] = [] | ||
return df | ||
assert_frame_equal(f(), expected) | ||
def f(): | ||
df = DataFrame() | ||
df['foo'] = df.index | ||
df['foo'] = Series(range(len(df))) | ||
return df | ||
assert_frame_equal(f(), expected) | ||
def f(): | ||
|
@@ -3429,21 +3433,31 @@ def f(): | |
|
||
# GH5720, GH5744 | ||
# don't create rows when empty | ||
expected = DataFrame(columns=['A','B','New']) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. note you can also contruct this like (you can change if you want)
|
||
expected['A'] = expected['A'].astype('int64') | ||
expected['B'] = expected['B'].astype('float64') | ||
expected['New'] = expected['New'].astype('float64') | ||
df = DataFrame({"A": [1, 2, 3], "B": [1.2, 4.2, 5.2]}) | ||
y = df[df.A > 5] | ||
y['New'] = np.nan | ||
assert_frame_equal(y,DataFrame(columns=['A','B','New'])) | ||
assert_frame_equal(y,expected) | ||
#assert_frame_equal(y,expected) | ||
|
||
expected = DataFrame(columns=['a','b','c c','d']) | ||
expected['d'] = expected['d'].astype('int64') | ||
df = DataFrame(columns=['a', 'b', 'c c']) | ||
df['d'] = 3 | ||
assert_frame_equal(df,DataFrame(columns=['a','b','c c','d'])) | ||
assert_frame_equal(df,expected) | ||
assert_series_equal(df['c c'],Series(name='c c',dtype=object)) | ||
|
||
# reindex columns is ok | ||
df = DataFrame({"A": [1, 2, 3], "B": [1.2, 4.2, 5.2]}) | ||
y = df[df.A > 5] | ||
result = y.reindex(columns=['A','B','C']) | ||
expected = DataFrame(columns=['A','B','C']) | ||
expected['A'] = expected['A'].astype('int64') | ||
expected['B'] = expected['B'].astype('float64') | ||
expected['C'] = expected['C'].astype('float64') | ||
assert_frame_equal(result,expected) | ||
|
||
# GH 5756 | ||
|
@@ -4429,6 +4443,15 @@ def test_indexing_assignment_dict_already_exists(self): | |
expected.loc[5] = [9, 99] | ||
tm.assert_frame_equal(df, expected) | ||
|
||
def test_indexing_dtypes_on_empty(self): | ||
# Check that .iloc and .ix return correct dtypes GH9983 | ||
df = DataFrame({'a':[1,2,3],'b':['b','b2','b3']}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. need a comment with the issue number |
||
df2 = df.ix[[],:] | ||
|
||
self.assertEqual(df2.loc[:,'a'].dtype, int) | ||
assert_series_equal(df2.loc[:,'a'], df2.iloc[:,0]) | ||
assert_series_equal(df2.loc[:,'a'], df2.ix[:,0]) | ||
|
||
|
||
|
||
class TestCategoricalIndex(tm.TestCase): | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -215,6 +215,14 @@ def test_multiindex_dtype(self): | |
{'a':[1.0,2.0],'b':[2.1,1.5],'c':['l1','l2']}, index=['a','b']) | ||
self._assert_not_equal(df1, df2, check_index_type=True) | ||
|
||
def test_empty_dtypes(self): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. move test to test_indexing.py (next to one from above) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are you sure? This is testing There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
ok, I see what you are trying to test here. But its circular, and not convincing that you are actually checking anything. I appreciate that you are trying to make sure that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I actually put this test in because it was asked for by @hayd . There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this is the right place (TestAssertFrameEqual) for a regression test of assert_frame_equals, which is what this is... ? |
||
df1=pd.DataFrame(columns=["col1","col2"]) | ||
df1["col1"] = df1["col1"].astype('int64') | ||
df2=pd.DataFrame(columns=["col1","col2"]) | ||
self._assert_equal(df1, df2, check_dtype=False) | ||
self._assert_not_equal(df1, df2, check_dtype=True) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok then. I would actually use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's in class |
||
|
||
|
||
class TestRNGContext(unittest.TestCase): | ||
|
||
def test_RNGContext(self): | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wow, this seems bad that this test passes at all.
df.to_json() == {}
here!!There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe
df.to_json() == '{"jim":{},"joe":{}}'
. It is checking that the number / names of columns is correct.