diff --git a/ci/requirements-3.6.run b/ci/requirements-3.6.run index b9d543f557d06..684dab333648a 100644 --- a/ci/requirements-3.6.run +++ b/ci/requirements-3.6.run @@ -1,3 +1,4 @@ python-dateutil pytz numpy +scipy diff --git a/pandas/core/groupby.py b/pandas/core/groupby.py index 950ad53abe5e0..7eba32b4932d0 100644 --- a/pandas/core/groupby.py +++ b/pandas/core/groupby.py @@ -2466,7 +2466,7 @@ def is_in_obj(gpr): "Defaulting to column but " "this will raise an ambiguity error in a " "future version") % gpr, - FutureWarning, stacklevel=2) + FutureWarning, stacklevel=5) in_axis, name, gpr = True, gpr, obj[gpr] exclusions.append(name) elif gpr in obj.index.names: @@ -4025,7 +4025,9 @@ def __iter__(self): sdata = self._get_sorted_data() if self.ngroups == 0: - raise StopIteration + # we are inside a generator, rather than raise StopIteration + # we merely return signal the end + return starts, ends = lib.generate_slices(self.slabels, self.ngroups) diff --git a/pandas/io/parsers.py b/pandas/io/parsers.py index 8e4246787ed5b..fdd753d1870b9 100755 --- a/pandas/io/parsers.py +++ b/pandas/io/parsers.py @@ -385,14 +385,20 @@ def _read(filepath_or_buffer, kwds): raise NotImplementedError("'nrows' and 'chunksize' cannot be used" " together yet.") elif nrows is not None: - data = parser.read(nrows) - parser.close() + try: + data = parser.read(nrows) + finally: + parser.close() return data + elif chunksize or iterator: return parser - data = parser.read() - parser.close() + try: + data = parser.read() + finally: + parser.close() + return data _parser_defaults = { diff --git a/pandas/io/stata.py b/pandas/io/stata.py index c35e07be2c31a..512a224555577 100644 --- a/pandas/io/stata.py +++ b/pandas/io/stata.py @@ -169,10 +169,13 @@ def read_stata(filepath_or_buffer, convert_dates=True, if iterator or chunksize: data = reader else: - data = reader.read() - reader.close() + try: + data = reader.read() + finally: + reader.close() return data + _date_formats = ["%tc", "%tC", "%td", "%d", "%tw", "%tm", "%tq", "%th", "%ty"] diff --git a/pandas/io/tests/parser/test_unsupported.py b/pandas/io/tests/parser/test_unsupported.py index ffd1cfa9a2538..64f31a11440d8 100644 --- a/pandas/io/tests/parser/test_unsupported.py +++ b/pandas/io/tests/parser/test_unsupported.py @@ -69,9 +69,9 @@ def test_c_engine(self): msg = 'Error tokenizing data' with tm.assertRaisesRegexp(ParserError, msg): - read_table(StringIO(text), sep='\s+') + read_table(StringIO(text), sep='\\s+') with tm.assertRaisesRegexp(ParserError, msg): - read_table(StringIO(text), engine='c', sep='\s+') + read_table(StringIO(text), engine='c', sep='\\s+') msg = "Only length-1 thousands markers supported" data = """A|B|C diff --git a/pandas/sparse/tests/test_array.py b/pandas/sparse/tests/test_array.py index bd896ae5b86d9..592926f8e821d 100644 --- a/pandas/sparse/tests/test_array.py +++ b/pandas/sparse/tests/test_array.py @@ -361,7 +361,7 @@ def test_astype(self): arr.astype('i8') arr = SparseArray([0, np.nan, 0, 1], fill_value=0) - msg = 'Cannot convert non-finite values \(NA or inf\) to integer' + msg = 'Cannot convert non-finite values \\(NA or inf\\) to integer' with tm.assertRaisesRegexp(ValueError, msg): arr.astype('i8') @@ -708,7 +708,7 @@ def test_cumsum(self): tm.assert_sp_array_equal(out, expected) axis = 1 # SparseArray currently 1-D, so only axis = 0 is valid. - msg = "axis\(={axis}\) out of bounds".format(axis=axis) + msg = "axis\\(={axis}\\) out of bounds".format(axis=axis) with tm.assertRaisesRegexp(ValueError, msg): SparseArray(data).cumsum(axis=axis) diff --git a/pandas/tests/frame/test_dtypes.py b/pandas/tests/frame/test_dtypes.py index 43a108e9acc80..ca757a821910a 100644 --- a/pandas/tests/frame/test_dtypes.py +++ b/pandas/tests/frame/test_dtypes.py @@ -390,7 +390,7 @@ def test_astype_cast_nan_inf_int(self): # GH14265, check nan and inf raise error when converting to int types = [np.int32, np.int64] values = [np.nan, np.inf] - msg = 'Cannot convert non-finite values \(NA or inf\) to integer' + msg = 'Cannot convert non-finite values \\(NA or inf\\) to integer' for this_type in types: for this_val in values: diff --git a/pandas/tests/series/test_dtypes.py b/pandas/tests/series/test_dtypes.py index 3eafbaf912797..f02b3168b233f 100644 --- a/pandas/tests/series/test_dtypes.py +++ b/pandas/tests/series/test_dtypes.py @@ -46,7 +46,7 @@ def test_astype_cast_nan_inf_int(self): # GH14265, check nan and inf raise error when converting to int types = [np.int32, np.int64] values = [np.nan, np.inf] - msg = 'Cannot convert non-finite values \(NA or inf\) to integer' + msg = 'Cannot convert non-finite values \\(NA or inf\\) to integer' for this_type in types: for this_val in values: diff --git a/pandas/tools/tests/test_pivot.py b/pandas/tools/tests/test_pivot.py index 5e800c02c9509..e63cfcc8c0590 100644 --- a/pandas/tools/tests/test_pivot.py +++ b/pandas/tools/tests/test_pivot.py @@ -381,21 +381,26 @@ def _check_output(result, values_col, index=['A', 'B'], df = df.set_index(['JOB', 'NAME', 'YEAR', 'MONTH'], drop=False, append=False) - result = df.pivot_table(index=['JOB', 'NAME'], - columns=['YEAR', 'MONTH'], - values=['DAYS', 'SALARY'], - aggfunc={'DAYS': 'mean', 'SALARY': 'sum'}, - margins=True) - - expected = df.pivot_table(index=['JOB', 'NAME'], - columns=['YEAR', 'MONTH'], values=['DAYS'], - aggfunc='mean', margins=True) + with tm.assert_produces_warning(FutureWarning, check_stacklevel=False): + result = df.pivot_table(index=['JOB', 'NAME'], + columns=['YEAR', 'MONTH'], + values=['DAYS', 'SALARY'], + aggfunc={'DAYS': 'mean', 'SALARY': 'sum'}, + margins=True) + + with tm.assert_produces_warning(FutureWarning, check_stacklevel=False): + expected = df.pivot_table(index=['JOB', 'NAME'], + columns=['YEAR', 'MONTH'], + values=['DAYS'], + aggfunc='mean', margins=True) tm.assert_frame_equal(result['DAYS'], expected['DAYS']) - expected = df.pivot_table(index=['JOB', 'NAME'], - columns=['YEAR', 'MONTH'], values=['SALARY'], - aggfunc='sum', margins=True) + with tm.assert_produces_warning(FutureWarning, check_stacklevel=False): + expected = df.pivot_table(index=['JOB', 'NAME'], + columns=['YEAR', 'MONTH'], + values=['SALARY'], + aggfunc='sum', margins=True) tm.assert_frame_equal(result['SALARY'], expected['SALARY'])