diff --git a/doc/source/v0.15.0.txt b/doc/source/v0.15.0.txt index aa6f1ce28a90d..34e9c7144607a 100644 --- a/doc/source/v0.15.0.txt +++ b/doc/source/v0.15.0.txt @@ -327,6 +327,7 @@ Bug Fixes date-likes incorrectly (:issue:`7762`, :issue:`7032`). +- Bug in ``Series.str.cat`` with an index which was filtered as to not include the first item (:issue:`7857`) diff --git a/pandas/core/strings.py b/pandas/core/strings.py index 3e730942ffc0e..2d8b8f8b2edff 100644 --- a/pandas/core/strings.py +++ b/pandas/core/strings.py @@ -12,7 +12,8 @@ def _get_array_list(arr, others): - if len(others) and isinstance(others[0], (list, np.ndarray)): + if len(others) and isinstance(_values_from_object(others)[0], + (list, np.ndarray, Series)): arrays = [arr] + list(others) else: arrays = [arr, others] diff --git a/pandas/tests/test_strings.py b/pandas/tests/test_strings.py index 971d7acf73027..41594a1655d18 100644 --- a/pandas/tests/test_strings.py +++ b/pandas/tests/test_strings.py @@ -16,7 +16,7 @@ from pandas.compat import range, lrange, u import pandas.compat as compat from pandas import (Index, Series, TimeSeries, DataFrame, isnull, notnull, - bdate_range, date_range) + bdate_range, date_range, MultiIndex) import pandas.core.common as com from pandas.util.testing import assert_series_equal, assert_almost_equal @@ -1193,6 +1193,23 @@ def test_encode_decode_errors(self): tm.assert_series_equal(result, exp) + def test_cat_on_filtered_index(self): + df = DataFrame(index=MultiIndex.from_product([[2011, 2012], [1,2,3]], + names=['year', 'month'])) + + df = df.reset_index() + df = df[df.month > 1] + + str_year = df.year.astype('str') + str_month = df.month.astype('str') + str_both = str_year.str.cat(str_month, sep=' ') + + self.assertEqual(str_both.loc[1], '2011 2') + + str_multiple = str_year.str.cat([str_month, str_month], sep=' ') + + self.assertEqual(str_multiple.loc[1], '2011 2 2') + if __name__ == '__main__': nose.runmodule(argv=[__file__, '-vvs', '-x', '--pdb', '--pdb-failure'],