Skip to content

ENH: allow axis argument to append / move append code to generic.py (GH8295) #8337

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

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
102 changes: 51 additions & 51 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -3656,57 +3656,57 @@ def infer(x):
#----------------------------------------------------------------------
# Merging / joining methods

def append(self, other, ignore_index=False, verify_integrity=False):
"""
Append columns of other to end of this frame's columns and index,
returning a new object. Columns not in this frame are added as new
columns.

Parameters
----------
other : DataFrame or list of Series/dict-like objects
ignore_index : boolean, default False
If True do not use the index labels. Useful for gluing together
record arrays
verify_integrity : boolean, default False
If True, raise ValueError on creating index with duplicates

Notes
-----
If a list of dict is passed and the keys are all contained in the
DataFrame's index, the order of the columns in the resulting DataFrame
will be unchanged

Returns
-------
appended : DataFrame
"""
if isinstance(other, (Series, dict)):
if isinstance(other, dict):
other = Series(other)
if other.name is None and not ignore_index:
raise TypeError('Can only append a Series if '
'ignore_index=True')

index = None if other.name is None else [other.name]
combined_columns = self.columns.tolist() + (self.columns | other.index).difference(self.columns).tolist()
other = other.reindex(combined_columns, copy=False)
other = DataFrame(other.values.reshape((1, len(other))),
index=index, columns=combined_columns).convert_objects()
if not self.columns.equals(combined_columns):
self = self.reindex(columns=combined_columns)
elif isinstance(other, list) and not isinstance(other[0], DataFrame):
other = DataFrame(other)
if (self.columns.get_indexer(other.columns) >= 0).all():
other = other.ix[:, self.columns]

from pandas.tools.merge import concat
if isinstance(other, (list, tuple)):
to_concat = [self] + other
else:
to_concat = [self, other]
return concat(to_concat, ignore_index=ignore_index,
verify_integrity=verify_integrity)
# def append(self, other, ignore_index=False, verify_integrity=False):
# """
# Append columns of other to end of this frame's columns and index,
Copy link
Contributor

Choose a reason for hiding this comment

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

simply delete this code (rather than comment it out).

# returning a new object. Columns not in this frame are added as new
# columns.
#
# Parameters
# ----------
# other : DataFrame or list of Series/dict-like objects
# ignore_index : boolean, default False
# If True do not use the index labels. Useful for gluing together
# record arrays
# verify_integrity : boolean, default False
# If True, raise ValueError on creating index with duplicates
#
# Notes
# -----
# If a list of dict is passed and the keys are all contained in the
# DataFrame's index, the order of the columns in the resulting DataFrame
# will be unchanged
#
# Returns
# -------
# appended : DataFrame
# """
# if isinstance(other, (Series, dict)):
# if isinstance(other, dict):
# other = Series(other)
# if other.name is None and not ignore_index:
# raise TypeError('Can only append a Series if '
# 'ignore_index=True')
#
# index = None if other.name is None else [other.name]
# combined_columns = self.columns.tolist() + ((self.columns | other.index) - self.columns).tolist()
# other = other.reindex(combined_columns, copy=False)
# other = DataFrame(other.values.reshape((1, len(other))),
# index=index, columns=combined_columns).convert_objects()
# if not self.columns.equals(combined_columns):
# self = self.reindex(columns=combined_columns)
# elif isinstance(other, list) and not isinstance(other[0], DataFrame):
# other = DataFrame(other)
# if (self.columns.get_indexer(other.columns) >= 0).all():
# other = other.ix[:, self.columns]
#
# from pandas.tools.merge import concat
# if isinstance(other, (list, tuple)):
# to_concat = [self] + other
# else:
# to_concat = [self, other]
# return concat(to_concat, ignore_index=ignore_index,
# verify_integrity=verify_integrity)

def join(self, other, on=None, how='left', lsuffix='', rsuffix='',
sort=False):
Expand Down
35 changes: 35 additions & 0 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -3618,6 +3618,41 @@ def _tz_localize(ax, tz, ambiguous):
result.set_axis(axis,ax)
return result.__finalize__(self)

#----------------------------------------------------------------------
# Merging / joining methods

def append(self, other, ignore_index=False, verify_integrity=False, axis=0):
if self.ndim >= 3:
msg = "append is not implemented on on Panel or PanelND objects."
raise NotImplementedError(msg)

if isinstance(other, (pd.Series, dict)):
if isinstance(other, dict):
other = pd.Series(other)
if other.name is None and not ignore_index:
raise TypeError('Can only append a Series if '
'ignore_index=True')

index = None if other.name is None else [other.name]
combined_columns = self.columns.tolist() + ((self.columns | other.index) - self.columns).tolist()
other = other.reindex(combined_columns, copy=False)
other = pd.DataFrame(other.values.reshape((1, len(other))),
index=index, columns=combined_columns).convert_objects()
if not self.columns.equals(combined_columns):
self = self.reindex(columns=combined_columns)
elif isinstance(other, list) and not isinstance(other[0], pd.DataFrame):
other = pd.DataFrame(other)
if (self.columns.get_indexer(other.columns) >= 0).all():
other = other.ix[:, self.columns]

from pandas.tools.merge import concat
if isinstance(other, (list, tuple)):
to_concat = [self] + other
else:
to_concat = [self, other]
return concat(to_concat, ignore_index=ignore_index, axis=axis,
verify_integrity=verify_integrity)

#----------------------------------------------------------------------
# Numeric Methods
def abs(self):
Expand Down
122 changes: 0 additions & 122 deletions pandas/tests/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -6606,128 +6606,6 @@ def test_convert_objects_no_conversion(self):
mixed2 = mixed1.convert_objects()
assert_frame_equal(mixed1, mixed2)

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

series = df.ix[4]
with assertRaisesRegexp(ValueError, 'Indexes have overlapping values'):
df.append(series, verify_integrity=True)
series.name = None
with assertRaisesRegexp(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)
assert_frame_equal(result, expected.ix[:, result.columns])

# can append when name set
row = df.ix[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)
expected = df.append(DataFrame(dicts), ignore_index=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_asfreq(self):
offset_monthly = self.tsframe.asfreq(datetools.bmonthEnd)
rule_monthly = self.tsframe.asfreq('BM')
Expand Down
Loading