Skip to content
This repository was archived by the owner on Dec 22, 2019. It is now read-only.

Deprecating sort=None #6

Open
wants to merge 17 commits into
base: secondtry
Choose a base branch
from
3 changes: 3 additions & 0 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -6338,6 +6338,9 @@ def append(self, other, ignore_index=False,
3 3
4 4
"""
if sort is not False and sort is not True:
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this before merge

raise ValueError("Sort is {!r}.".format(sort))

kwargs = {
'ignore_index': ignore_index,
'verify_integrity': verify_integrity,
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ def _setitem_with_indexer(self, indexer, value):
value = Series(value, index=self.obj.columns,
name=indexer)

self.obj._data = self.obj.append(value)._data
self.obj._data = self.obj.append(value, sort=False)._data
self.obj._maybe_update_cacher(clear=True)
return self.obj

Expand Down
6 changes: 3 additions & 3 deletions pandas/core/reshape/pivot.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ def _add_margins(table, data, values, rows, cols, aggfunc,
for dtype in set(result.dtypes):
cols = result.select_dtypes([dtype]).columns
margin_dummy[cols] = margin_dummy[cols].astype(dtype)
result = result.append(margin_dummy)
result = result.append(margin_dummy, sort=False)
except TypeError:

# we cannot reshape, so coerce the axis
Expand Down Expand Up @@ -583,15 +583,15 @@ def _normalize(table, normalize, margins, margins_name='All'):

elif normalize == 'index':
index_margin = index_margin / index_margin.sum()
table = table.append(index_margin)
table = table.append(index_margin, sort=False)
table = table.fillna(0)

elif normalize == "all" or normalize is True:
column_margin = column_margin / column_margin.sum()
index_margin = index_margin / index_margin.sum()
index_margin.loc[margins_name] = 1
table = concat([table, column_margin], axis=1)
table = table.append(index_margin)
table = table.append(index_margin, sort=False)

table = table.fillna(0)

Expand Down
127 changes: 0 additions & 127 deletions pandas/tests/frame/test_combine_concat.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,133 +71,6 @@ def test_concat_tuple_keys(self):
('bee', 'boo', 2): 2.0}})
assert_frame_equal(results, expected)

def test_append_series_dict(self):
df = DataFrame(np.random.randn(5, 4),
columns=['foo', 'bar', 'baz', 'qux'])

series = df.loc[4]
with tm.assert_raises_regex(ValueError,
'Indexes have overlapping values'):
df.append(series, verify_integrity=True)
series.name = None
with tm.assert_raises_regex(TypeError,
'Can only append a Series if '
'ignore_index=True'):
df.append(series, verify_integrity=True)

result = df.append(series[::-1], ignore_index=True)
expected = df.append(DataFrame({0: series[::-1]}, index=df.columns).T,
ignore_index=True)
assert_frame_equal(result, expected)

# dict
result = df.append(series.to_dict(), ignore_index=True)
assert_frame_equal(result, expected)

result = df.append(series[::-1][:3], ignore_index=True)
expected = df.append(DataFrame({0: series[::-1][:3]}).T,
ignore_index=True, sort=True)
assert_frame_equal(result, expected.loc[:, result.columns])

# can append when name set
row = df.loc[4]
row.name = 5
result = df.append(row)
expected = df.append(df[-1:], ignore_index=True)
assert_frame_equal(result, expected)

def test_append_list_of_series_dicts(self):
df = DataFrame(np.random.randn(5, 4),
columns=['foo', 'bar', 'baz', 'qux'])

dicts = [x.to_dict() for idx, x in df.iterrows()]

result = df.append(dicts, ignore_index=True)
expected = df.append(df, ignore_index=True)
assert_frame_equal(result, expected)

# different columns
dicts = [{'foo': 1, 'bar': 2, 'baz': 3, 'peekaboo': 4},
{'foo': 5, 'bar': 6, 'baz': 7, 'peekaboo': 8}]
result = df.append(dicts, ignore_index=True, sort=True)
expected = df.append(DataFrame(dicts), ignore_index=True, sort=True)
assert_frame_equal(result, expected)

def test_append_empty_dataframe(self):

# Empty df append empty df
df1 = DataFrame([])
df2 = DataFrame([])
result = df1.append(df2)
expected = df1.copy()
assert_frame_equal(result, expected)

# Non-empty df append empty df
df1 = DataFrame(np.random.randn(5, 2))
df2 = DataFrame()
result = df1.append(df2)
expected = df1.copy()
assert_frame_equal(result, expected)

# Empty df with columns append empty df
df1 = DataFrame(columns=['bar', 'foo'])
df2 = DataFrame()
result = df1.append(df2)
expected = df1.copy()
assert_frame_equal(result, expected)

# Non-Empty df with columns append empty df
df1 = DataFrame(np.random.randn(5, 2), columns=['bar', 'foo'])
df2 = DataFrame()
result = df1.append(df2)
expected = df1.copy()
assert_frame_equal(result, expected)

def test_append_dtypes(self):

# GH 5754
# row appends of different dtypes (so need to do by-item)
# can sometimes infer the correct type

df1 = DataFrame({'bar': Timestamp('20130101')}, index=lrange(5))
df2 = DataFrame()
result = df1.append(df2)
expected = df1.copy()
assert_frame_equal(result, expected)

df1 = DataFrame({'bar': Timestamp('20130101')}, index=lrange(1))
df2 = DataFrame({'bar': 'foo'}, index=lrange(1, 2))
result = df1.append(df2)
expected = DataFrame({'bar': [Timestamp('20130101'), 'foo']})
assert_frame_equal(result, expected)

df1 = DataFrame({'bar': Timestamp('20130101')}, index=lrange(1))
df2 = DataFrame({'bar': np.nan}, index=lrange(1, 2))
result = df1.append(df2)
expected = DataFrame(
{'bar': Series([Timestamp('20130101'), np.nan], dtype='M8[ns]')})
assert_frame_equal(result, expected)

df1 = DataFrame({'bar': Timestamp('20130101')}, index=lrange(1))
df2 = DataFrame({'bar': np.nan}, index=lrange(1, 2), dtype=object)
result = df1.append(df2)
expected = DataFrame(
{'bar': Series([Timestamp('20130101'), np.nan], dtype='M8[ns]')})
assert_frame_equal(result, expected)

df1 = DataFrame({'bar': np.nan}, index=lrange(1))
df2 = DataFrame({'bar': Timestamp('20130101')}, index=lrange(1, 2))
result = df1.append(df2)
expected = DataFrame(
{'bar': Series([np.nan, Timestamp('20130101')], dtype='M8[ns]')})
assert_frame_equal(result, expected)

df1 = DataFrame({'bar': Timestamp('20130101')}, index=lrange(1))
df2 = DataFrame({'bar': 1}, index=lrange(1, 2), dtype=object)
result = df1.append(df2)
expected = DataFrame({'bar': Series([Timestamp('20130101'), 1])})
assert_frame_equal(result, expected)

def test_update(self):
df = DataFrame([[1.5, nan, 3.],
[1.5, nan, 3.],
Expand Down
5 changes: 3 additions & 2 deletions pandas/tests/frame/test_duplicates.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,9 @@ def test_drop_duplicates():
tm.assert_frame_equal(df.drop_duplicates(), df)

# GH 11864
df = DataFrame([i] * 9 for i in range(16))
df = df.append([[1] + [0] * 8], ignore_index=True)
df1 = DataFrame([i] * 9 for i in range(16))
df2 = DataFrame([[1] + [0] * 8])
df = df1.append(df2, ignore_index=True, sort=False)

for keep in ['first', 'last', False]:
assert df.duplicated(keep=keep).sum() == 0
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/indexes/datetimes/test_datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ def test_append_numpy_bug_1681(self):
a = DataFrame()
c = DataFrame({'A': 'foo', 'B': dr}, index=dr)

result = a.append(c)
result = a.append(c, sort=False)
assert (result['B'] == dr).all()

def test_isin(self):
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/indexes/timedeltas/test_timedelta.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ def test_append_numpy_bug_1681(self):
c = DataFrame({'A': 'foo', 'B': td}, index=td)
str(c)

result = a.append(c)
result = a.append(c, sort=False)
assert (result['B'] == td).all()

def test_fields(self):
Expand Down
11 changes: 5 additions & 6 deletions pandas/tests/indexing/test_partial.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,15 +184,16 @@ def test_partial_setting_mixed_dtype(self):

s = df.loc[1].copy()
s.name = 2
expected = df.append(s)
expected = df.append(s, sort=False)

df.loc[2] = df.loc[1]
tm.assert_frame_equal(df, expected)

# columns will align
df = DataFrame(columns=['A', 'B'])
df.loc[0] = Series(1, index=range(4))
tm.assert_frame_equal(df, DataFrame(columns=['A', 'B'], index=[0]))
exp = DataFrame(columns=['A', 'B'], index=[0], dtype='float64')
tm.assert_frame_equal(df, exp)

# columns will align
df = DataFrame(columns=['A', 'B'])
Expand All @@ -210,12 +211,10 @@ def f():

pytest.raises(ValueError, f)

# TODO: #15657, these are left as object and not coerced
df = DataFrame(columns=['A', 'B'])
df.loc[3] = [6, 7]

exp = DataFrame([[6, 7]], index=[3], columns=['A', 'B'],
dtype='object')
exp = DataFrame([[6, 7]], index=[3], columns=['A', 'B'])
tm.assert_frame_equal(df, exp)

def test_series_partial_set(self):
Expand Down Expand Up @@ -441,7 +440,7 @@ def f():
df = orig.copy()
with catch_warnings(record=True):
df.loc['a', :] = df.ix[0]
exp = orig.append(Series(df.ix[0], name='a'))
exp = orig.append(Series(df.ix[0], name='a'), sort=False)
tm.assert_frame_equal(df, exp)
tm.assert_index_equal(df.index, Index(orig.index.tolist() + ['a']))
assert df.index.dtype == 'object'
Expand Down
16 changes: 10 additions & 6 deletions pandas/tests/reshape/merge/test_merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -489,8 +489,8 @@ def test_join_append_timedeltas(self):

d = {'d': dt.datetime(2013, 11, 5, 5, 56), 't': dt.timedelta(0, 22500)}
df = DataFrame(columns=list('dt'))
df = df.append(d, ignore_index=True)
result = df.append(d, ignore_index=True)
df = df.append(d, ignore_index=True, sort=False)
result = df.append(d, ignore_index=True, sort=False)
expected = DataFrame({'d': [dt.datetime(2013, 11, 5, 5, 56),
dt.datetime(2013, 11, 5, 5, 56)],
't': [dt.timedelta(0, 22500),
Expand Down Expand Up @@ -813,8 +813,10 @@ def test_validation(self):
assert_frame_equal(result, expected_3)

# Dups on right
right_w_dups = right.append(pd.DataFrame({'a': ['e'], 'c': ['moo']},
index=[4]))
right_w_dups = right.append(
pd.DataFrame({'a': ['e'], 'c': ['moo']}, index=[4]),
sort=False
)
merge(left, right_w_dups, left_index=True, right_index=True,
validate='one_to_many')

Expand All @@ -826,8 +828,10 @@ def test_validation(self):
merge(left, right_w_dups, on='a', validate='one_to_one')

# Dups on left
left_w_dups = left.append(pd.DataFrame({'a': ['a'], 'c': ['cow']},
index=[3]), sort=True)
left_w_dups = left.append(
pd.DataFrame({'a': ['a'], 'c': ['cow']}, index=[3]),
sort=True
)
merge(left_w_dups, right, left_index=True, right_index=True,
validate='many_to_one')

Expand Down
Loading