|
1 | 1 | # -*- coding: utf-8 -*-
|
2 | 2 | from __future__ import print_function
|
| 3 | + |
3 | 4 | import re
|
| 5 | +import sys |
4 | 6 | from datetime import datetime, timedelta
|
| 7 | + |
5 | 8 | import numpy as np
|
6 |
| -import pandas.compat as compat |
| 9 | + |
7 | 10 | import pandas as pd
|
8 |
| -from pandas.compat import u, StringIO |
9 |
| -from pandas.core.base import FrozenList, FrozenNDArray, PandasDelegate, NoNewAttributesMixin |
| 11 | +import pandas.compat as compat |
10 | 12 | import pandas.core.common as com
|
| 13 | +import pandas.util.testing as tm |
| 14 | +from pandas import (Series, Index, DatetimeIndex, |
| 15 | + TimedeltaIndex, PeriodIndex, Timedelta) |
| 16 | +from pandas.compat import u, StringIO |
| 17 | +from pandas.core.base import (FrozenList, FrozenNDArray, |
| 18 | + PandasDelegate, NoNewAttributesMixin) |
11 | 19 | from pandas.tseries.base import DatetimeIndexOpsMixin
|
12 |
| -from pandas.util.testing import assertRaisesRegexp, assertIsInstance |
13 |
| -from pandas.tseries.common import is_datetimelike |
14 |
| -from pandas import Series, Index, Int64Index, DatetimeIndex, TimedeltaIndex, PeriodIndex, Timedelta |
15 |
| -import pandas.tslib as tslib |
16 |
| -from pandas import _np_version_under1p9 |
17 |
| -import nose |
| 20 | +from pandas.util.testing import (assertRaisesRegexp, |
| 21 | + assertIsInstance) |
18 | 22 |
|
19 |
| -import pandas.util.testing as tm |
20 | 23 |
|
21 | 24 | class CheckStringMixin(object):
|
22 | 25 | def test_string_methods_dont_fail(self):
|
@@ -112,7 +115,9 @@ def setUp(self):
|
112 | 115 | def test_shallow_copying(self):
|
113 | 116 | original = self.container.copy()
|
114 | 117 | assertIsInstance(self.container.view(), FrozenNDArray)
|
115 |
| - self.assertFalse(isinstance(self.container.view(np.ndarray), FrozenNDArray)) |
| 118 | + self.assertFalse(isinstance( |
| 119 | + self.container.view(np.ndarray), FrozenNDArray |
| 120 | + )) |
116 | 121 | self.assertIsNot(self.container.view(), self.container)
|
117 | 122 | self.assert_numpy_array_equal(self.container, original)
|
118 | 123 | # shallow copy should be the same too
|
@@ -881,27 +886,30 @@ def get_fill_value(obj):
|
881 | 886 | # check shallow_copied
|
882 | 887 | self.assertFalse(o is result)
|
883 | 888 |
|
884 |
| - |
885 | 889 | def test_memory_usage(self):
|
886 | 890 | for o in self.objs:
|
887 | 891 | res = o.memory_usage()
|
888 |
| - res2 = o.memory_usage(deep=True) |
| 892 | + res_deep = o.memory_usage(deep=True) |
889 | 893 |
|
890 |
| - if com.is_object_dtype(o): |
891 |
| - self.assertTrue(res2 > res) |
| 894 | + if (com.is_object_dtype(o) or |
| 895 | + (isinstance(o, Series) and |
| 896 | + com.is_object_dtype(o.index))): |
| 897 | + # if there are objects, only deep will pick them up |
| 898 | + self.assertTrue(res_deep > res) |
892 | 899 | else:
|
893 |
| - self.assertEqual(res, res2) |
| 900 | + self.assertEqual(res, res_deep) |
894 | 901 |
|
895 | 902 | if isinstance(o, Series):
|
896 |
| - res = o.memory_usage(index=True) |
897 |
| - res2 = o.memory_usage(index=True, deep=True) |
898 |
| - if com.is_object_dtype(o) or com.is_object_dtype(o.index): |
899 |
| - self.assertTrue(res2 > res) |
900 |
| - else: |
901 |
| - self.assertEqual(res, res2) |
902 |
| - |
903 |
| - self.assertEqual(o.memory_usage(index=False) + o.index.memory_usage(), |
904 |
| - o.memory_usage(index=True)) |
| 903 | + self.assertEqual( |
| 904 | + (o.memory_usage(index=False) + |
| 905 | + o.index.memory_usage()), |
| 906 | + o.memory_usage(index=True) |
| 907 | + ) |
| 908 | + |
| 909 | + # sys.getsizeof will call the .memory_usage with |
| 910 | + # deep=True, and add on some GC overhead |
| 911 | + diff = res_deep - sys.getsizeof(o) |
| 912 | + self.assertTrue(abs(diff) < 100) |
905 | 913 |
|
906 | 914 |
|
907 | 915 | class TestFloat64HashTable(tm.TestCase):
|
|
0 commit comments