Skip to content

Commit d9b3340

Browse files
committed
Merge pull request #5584 from jreback/copy_warn
BUG/TST: reset setitem_copy on object enlargement
2 parents c73b957 + b922189 commit d9b3340

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

71 files changed

+453
-654
lines changed

pandas/computation/tests/test_eval.py

+12-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#!/usr/bin/env python
22

3-
import unittest
43
import functools
54
from itertools import product
65

@@ -104,10 +103,11 @@ def _is_py3_complex_incompat(result, expected):
104103
_good_arith_ops = com.difference(_arith_ops_syms, _special_case_arith_ops_syms)
105104

106105

107-
class TestEvalNumexprPandas(unittest.TestCase):
106+
class TestEvalNumexprPandas(tm.TestCase):
108107

109108
@classmethod
110109
def setUpClass(cls):
110+
super(TestEvalNumexprPandas, cls).setUpClass()
111111
skip_if_no_ne()
112112
import numexpr as ne
113113
cls.ne = ne
@@ -116,6 +116,7 @@ def setUpClass(cls):
116116

117117
@classmethod
118118
def tearDownClass(cls):
119+
super(TestEvalNumexprPandas, cls).tearDownClass()
119120
del cls.engine, cls.parser
120121
if hasattr(cls, 'ne'):
121122
del cls.ne
@@ -707,6 +708,7 @@ class TestEvalNumexprPython(TestEvalNumexprPandas):
707708

708709
@classmethod
709710
def setUpClass(cls):
711+
super(TestEvalNumexprPython, cls).setUpClass()
710712
skip_if_no_ne()
711713
import numexpr as ne
712714
cls.ne = ne
@@ -733,6 +735,7 @@ class TestEvalPythonPython(TestEvalNumexprPython):
733735

734736
@classmethod
735737
def setUpClass(cls):
738+
super(TestEvalPythonPython, cls).setUpClass()
736739
cls.engine = 'python'
737740
cls.parser = 'python'
738741

@@ -761,6 +764,7 @@ class TestEvalPythonPandas(TestEvalPythonPython):
761764

762765
@classmethod
763766
def setUpClass(cls):
767+
super(TestEvalPythonPandas, cls).setUpClass()
764768
cls.engine = 'python'
765769
cls.parser = 'pandas'
766770

@@ -1024,17 +1028,19 @@ def test_performance_warning_for_poor_alignment(self):
10241028
#------------------------------------
10251029
# slightly more complex ops
10261030

1027-
class TestOperationsNumExprPandas(unittest.TestCase):
1031+
class TestOperationsNumExprPandas(tm.TestCase):
10281032

10291033
@classmethod
10301034
def setUpClass(cls):
1035+
super(TestOperationsNumExprPandas, cls).setUpClass()
10311036
skip_if_no_ne()
10321037
cls.engine = 'numexpr'
10331038
cls.parser = 'pandas'
10341039
cls.arith_ops = expr._arith_ops_syms + expr._cmp_ops_syms
10351040

10361041
@classmethod
10371042
def tearDownClass(cls):
1043+
super(TestOperationsNumExprPandas, cls).tearDownClass()
10381044
del cls.engine, cls.parser
10391045

10401046
def eval(self, *args, **kwargs):
@@ -1337,6 +1343,7 @@ class TestOperationsNumExprPython(TestOperationsNumExprPandas):
13371343

13381344
@classmethod
13391345
def setUpClass(cls):
1346+
super(TestOperationsNumExprPython, cls).setUpClass()
13401347
if not _USE_NUMEXPR:
13411348
raise nose.SkipTest("numexpr engine not installed")
13421349
cls.engine = 'numexpr'
@@ -1404,6 +1411,7 @@ class TestOperationsPythonPython(TestOperationsNumExprPython):
14041411

14051412
@classmethod
14061413
def setUpClass(cls):
1414+
super(TestOperationsPythonPython, cls).setUpClass()
14071415
cls.engine = cls.parser = 'python'
14081416
cls.arith_ops = expr._arith_ops_syms + expr._cmp_ops_syms
14091417
cls.arith_ops = filter(lambda x: x not in ('in', 'not in'),
@@ -1414,6 +1422,7 @@ class TestOperationsPythonPandas(TestOperationsNumExprPandas):
14141422

14151423
@classmethod
14161424
def setUpClass(cls):
1425+
super(TestOperationsPythonPandas, cls).setUpClass()
14171426
cls.engine = 'python'
14181427
cls.parser = 'pandas'
14191428
cls.arith_ops = expr._arith_ops_syms + expr._cmp_ops_syms

pandas/core/frame.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -1563,7 +1563,7 @@ def _ixs(self, i, axis=0, copy=False):
15631563

15641564
# a location index by definition
15651565
i = _maybe_convert_indices(i, len(self._get_axis(axis)))
1566-
return self.reindex(i, takeable=True)
1566+
return self.reindex(i, takeable=True)._setitem_copy(True)
15671567
else:
15681568
new_values, copy = self._data.fast_2d_xs(i, copy=copy)
15691569
return Series(new_values, index=self.columns,
@@ -2714,7 +2714,7 @@ def trans(v):
27142714

27152715
self._clear_item_cache()
27162716
else:
2717-
return self.take(indexer, axis=axis, convert=False)
2717+
return self.take(indexer, axis=axis, convert=False, is_copy=False)
27182718

27192719
def sortlevel(self, level=0, axis=0, ascending=True, inplace=False):
27202720
"""
@@ -2760,7 +2760,7 @@ def sortlevel(self, level=0, axis=0, ascending=True, inplace=False):
27602760

27612761
self._clear_item_cache()
27622762
else:
2763-
return self.take(indexer, axis=axis, convert=False)
2763+
return self.take(indexer, axis=axis, convert=False, is_copy=False)
27642764

27652765
def swaplevel(self, i, j, axis=0):
27662766
"""

pandas/core/generic.py

+12-6
Original file line numberDiff line numberDiff line change
@@ -1064,7 +1064,7 @@ def __delitem__(self, key):
10641064
except KeyError:
10651065
pass
10661066

1067-
def take(self, indices, axis=0, convert=True):
1067+
def take(self, indices, axis=0, convert=True, is_copy=True):
10681068
"""
10691069
Analogous to ndarray.take
10701070
@@ -1073,6 +1073,7 @@ def take(self, indices, axis=0, convert=True):
10731073
indices : list / array of ints
10741074
axis : int, default 0
10751075
convert : translate neg to pos indices (default)
1076+
is_copy : mark the returned frame as a copy
10761077
10771078
Returns
10781079
-------
@@ -1090,12 +1091,17 @@ def take(self, indices, axis=0, convert=True):
10901091
labels = self._get_axis(axis)
10911092
new_items = labels.take(indices)
10921093
new_data = self._data.reindex_axis(new_items, indexer=indices,
1093-
axis=0)
1094+
axis=baxis)
10941095
else:
1095-
new_data = self._data.take(indices, axis=baxis, verify=convert)
1096-
return self._constructor(new_data)\
1097-
._setitem_copy(True)\
1098-
.__finalize__(self)
1096+
new_data = self._data.take(indices, axis=baxis)
1097+
1098+
result = self._constructor(new_data).__finalize__(self)
1099+
1100+
# maybe set copy if we didn't actually change the index
1101+
if is_copy and not result._get_axis(axis).equals(self._get_axis(axis)):
1102+
result = result._setitem_copy(is_copy)
1103+
1104+
return result
10991105

11001106
# TODO: Check if this was clearer in 0.12
11011107
def select(self, crit, axis=0):

pandas/core/indexing.py

+1
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ def _setitem_with_indexer(self, indexer, value):
209209
labels = _safe_append_to_index(index, key)
210210
self.obj._data = self.obj.reindex_axis(labels, i)._data
211211
self.obj._maybe_update_cacher(clear=True)
212+
self.obj._setitem_copy(False)
212213

213214
if isinstance(labels, MultiIndex):
214215
self.obj.sortlevel(inplace=True)

pandas/io/tests/test_clipboard.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import unittest
2-
31
import numpy as np
42
from numpy.random import randint
53

@@ -18,9 +16,10 @@
1816
raise nose.SkipTest("no clipboard found")
1917

2018

21-
class TestClipboard(unittest.TestCase):
19+
class TestClipboard(tm.TestCase):
2220
@classmethod
2321
def setUpClass(cls):
22+
super(TestClipboard, cls).setUpClass()
2423
cls.data = {}
2524
cls.data['string'] = mkdf(5, 3, c_idx_type='s', r_idx_type='i',
2625
c_idx_names=[None], r_idx_names=[None])
@@ -43,6 +42,7 @@ def setUpClass(cls):
4342

4443
@classmethod
4544
def tearDownClass(cls):
45+
super(TestClipboard, cls).tearDownClass()
4646
del cls.data_types, cls.data
4747

4848
def check_round_trip_frame(self, data_type, excel=None, sep=None):

pandas/io/tests/test_cparser.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import os
1010
import sys
1111
import re
12-
import unittest
1312

1413
import nose
1514

@@ -32,7 +31,7 @@
3231
import pandas.parser as parser
3332

3433

35-
class TestCParser(unittest.TestCase):
34+
class TestCParser(tm.TestCase):
3635

3736
def setUp(self):
3837
self.dirpath = tm.get_data_path()
@@ -132,7 +131,7 @@ def test_integer_thousands(self):
132131

133132
expected = [123456, 12500]
134133
tm.assert_almost_equal(result[0], expected)
135-
134+
136135
def test_integer_thousands_alt(self):
137136
data = '123.456\n12.500'
138137

pandas/io/tests/test_data.py

+13-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from __future__ import print_function
22
from pandas import compat
3-
import unittest
43
import warnings
54
import nose
65
from nose.tools import assert_equal
@@ -39,15 +38,17 @@ def assert_n_failed_equals_n_null_columns(wngs, obj, cls=SymbolWarning):
3938
assert msgs.str.contains('|'.join(failed_symbols)).all()
4039

4140

42-
class TestGoogle(unittest.TestCase):
41+
class TestGoogle(tm.TestCase):
4342
@classmethod
4443
def setUpClass(cls):
44+
super(TestGoogle, cls).setUpClass()
4545
cls.locales = tm.get_locales(prefix='en_US')
4646
if not cls.locales:
4747
raise nose.SkipTest("US English locale not available for testing")
4848

4949
@classmethod
5050
def tearDownClass(cls):
51+
super(TestGoogle, cls).tearDownClass()
5152
del cls.locales
5253

5354
@network
@@ -109,9 +110,10 @@ def test_get_multi2(self):
109110
assert_n_failed_equals_n_null_columns(w, result)
110111

111112

112-
class TestYahoo(unittest.TestCase):
113+
class TestYahoo(tm.TestCase):
113114
@classmethod
114115
def setUpClass(cls):
116+
super(TestYahoo, cls).setUpClass()
115117
_skip_if_no_lxml()
116118

117119
@network
@@ -228,9 +230,10 @@ def test_get_date_ret_index(self):
228230
assert np.issubdtype(pan.values.dtype, np.floating)
229231

230232

231-
class TestYahooOptions(unittest.TestCase):
233+
class TestYahooOptions(tm.TestCase):
232234
@classmethod
233235
def setUpClass(cls):
236+
super(TestYahooOptions, cls).setUpClass()
234237
_skip_if_no_lxml()
235238

236239
# aapl has monthlies
@@ -245,6 +248,7 @@ def setUpClass(cls):
245248

246249
@classmethod
247250
def tearDownClass(cls):
251+
super(TestYahooOptions, cls).tearDownClass()
248252
del cls.aapl, cls.expiry
249253

250254
@network
@@ -287,9 +291,10 @@ def test_get_put_data(self):
287291
assert len(puts)>1
288292

289293

290-
class TestOptionsWarnings(unittest.TestCase):
294+
class TestOptionsWarnings(tm.TestCase):
291295
@classmethod
292296
def setUpClass(cls):
297+
super(TestOptionsWarnings, cls).setUpClass()
293298
_skip_if_no_lxml()
294299

295300
with assert_produces_warning(FutureWarning):
@@ -304,6 +309,7 @@ def setUpClass(cls):
304309

305310
@classmethod
306311
def tearDownClass(cls):
312+
super(TestOptionsWarnings, cls).tearDownClass()
307313
del cls.aapl, cls.year, cls.month
308314

309315
@network
@@ -346,7 +352,7 @@ def test_get_put_data_warning(self):
346352
warnings.warn("IndexError thrown no tables found")
347353

348354

349-
class TestDataReader(unittest.TestCase):
355+
class TestDataReader(tm.TestCase):
350356
def test_is_s3_url(self):
351357
from pandas.io.common import _is_s3_url
352358
self.assert_(_is_s3_url("s3://pandas/somethingelse.com"))
@@ -376,7 +382,7 @@ def test_read_famafrench(self):
376382
assert isinstance(ff, dict)
377383

378384

379-
class TestFred(unittest.TestCase):
385+
class TestFred(tm.TestCase):
380386
@network
381387
def test_fred(self):
382388
"""

pandas/io/tests/test_date_converters.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import os
55
import sys
66
import re
7-
import unittest
87

98
import nose
109

@@ -22,9 +21,9 @@
2221
from pandas import compat
2322
from pandas.lib import Timestamp
2423
import pandas.io.date_converters as conv
24+
import pandas.util.testing as tm
2525

26-
27-
class TestConverters(unittest.TestCase):
26+
class TestConverters(tm.TestCase):
2827

2928
def setUp(self):
3029
self.years = np.array([2007, 2008])

0 commit comments

Comments
 (0)