|
1 |
| -# -*- coding: utf-8 -*- |
| 1 | +# -*- coding: utf-8 -*- |
2 | 2 | from __future__ import print_function
|
3 | 3 | from distutils.version import LooseVersion
|
4 | 4 | import re
|
@@ -2969,128 +2969,6 @@ def test_to_csv_engine_kw_deprecation(self):
|
2969 | 2969 | df = DataFrame({'col1' : [1], 'col2' : ['a'], 'col3' : [10.1] })
|
2970 | 2970 | df.to_csv(engine='python')
|
2971 | 2971 |
|
2972 |
| - def test_round_dataframe(self): |
2973 |
| - |
2974 |
| - # GH 2665 |
2975 |
| - |
2976 |
| - # Test that rounding an empty DataFrame does nothing |
2977 |
| - df = DataFrame() |
2978 |
| - tm.assert_frame_equal(df, df.round()) |
2979 |
| - |
2980 |
| - # Here's the test frame we'll be working with |
2981 |
| - df = DataFrame( |
2982 |
| - {'col1': [1.123, 2.123, 3.123], 'col2': [1.234, 2.234, 3.234]}) |
2983 |
| - |
2984 |
| - # Default round to integer (i.e. decimals=0) |
2985 |
| - expected_rounded = DataFrame( |
2986 |
| - {'col1': [1., 2., 3.], 'col2': [1., 2., 3.]}) |
2987 |
| - tm.assert_frame_equal(df.round(), expected_rounded) |
2988 |
| - |
2989 |
| - # Round with an integer |
2990 |
| - decimals = 2 |
2991 |
| - expected_rounded = DataFrame( |
2992 |
| - {'col1': [1.12, 2.12, 3.12], 'col2': [1.23, 2.23, 3.23]}) |
2993 |
| - tm.assert_frame_equal(df.round(decimals), expected_rounded) |
2994 |
| - |
2995 |
| - # This should also work with np.round (since np.round dispatches to |
2996 |
| - # df.round) |
2997 |
| - tm.assert_frame_equal(np.round(df, decimals), expected_rounded) |
2998 |
| - |
2999 |
| - # Round with a list |
3000 |
| - round_list = [1, 2] |
3001 |
| - with self.assertRaises(TypeError): |
3002 |
| - df.round(round_list) |
3003 |
| - |
3004 |
| - # Round with a dictionary |
3005 |
| - expected_rounded = DataFrame( |
3006 |
| - {'col1': [1.1, 2.1, 3.1], 'col2': [1.23, 2.23, 3.23]}) |
3007 |
| - round_dict = {'col1': 1, 'col2': 2} |
3008 |
| - tm.assert_frame_equal(df.round(round_dict), expected_rounded) |
3009 |
| - |
3010 |
| - # Incomplete dict |
3011 |
| - expected_partially_rounded = DataFrame( |
3012 |
| - {'col1': [1.123, 2.123, 3.123], 'col2': [1.2, 2.2, 3.2]}) |
3013 |
| - partial_round_dict = {'col2': 1} |
3014 |
| - tm.assert_frame_equal( |
3015 |
| - df.round(partial_round_dict), expected_partially_rounded) |
3016 |
| - |
3017 |
| - # Dict with unknown elements |
3018 |
| - wrong_round_dict = {'col3': 2, 'col2': 1} |
3019 |
| - tm.assert_frame_equal( |
3020 |
| - df.round(wrong_round_dict), expected_partially_rounded) |
3021 |
| - |
3022 |
| - # float input to `decimals` |
3023 |
| - non_int_round_dict = {'col1': 1, 'col2': 0.5} |
3024 |
| - if sys.version < LooseVersion('2.7'): |
3025 |
| - # np.round([1.123, 2.123], 0.5) is only a warning in Python 2.6 |
3026 |
| - with self.assert_produces_warning(DeprecationWarning, check_stacklevel=False): |
3027 |
| - df.round(non_int_round_dict) |
3028 |
| - else: |
3029 |
| - with self.assertRaises(TypeError): |
3030 |
| - df.round(non_int_round_dict) |
3031 |
| - |
3032 |
| - # String input |
3033 |
| - non_int_round_dict = {'col1': 1, 'col2': 'foo'} |
3034 |
| - with self.assertRaises(TypeError): |
3035 |
| - df.round(non_int_round_dict) |
3036 |
| - |
3037 |
| - non_int_round_Series = Series(non_int_round_dict) |
3038 |
| - with self.assertRaises(TypeError): |
3039 |
| - df.round(non_int_round_Series) |
3040 |
| - |
3041 |
| - # List input |
3042 |
| - non_int_round_dict = {'col1': 1, 'col2': [1, 2]} |
3043 |
| - with self.assertRaises(TypeError): |
3044 |
| - df.round(non_int_round_dict) |
3045 |
| - |
3046 |
| - non_int_round_Series = Series(non_int_round_dict) |
3047 |
| - with self.assertRaises(TypeError): |
3048 |
| - df.round(non_int_round_Series) |
3049 |
| - |
3050 |
| - # Non integer Series inputs |
3051 |
| - non_int_round_Series = Series(non_int_round_dict) |
3052 |
| - with self.assertRaises(TypeError): |
3053 |
| - df.round(non_int_round_Series) |
3054 |
| - |
3055 |
| - non_int_round_Series = Series(non_int_round_dict) |
3056 |
| - with self.assertRaises(TypeError): |
3057 |
| - df.round(non_int_round_Series) |
3058 |
| - |
3059 |
| - # Negative numbers |
3060 |
| - negative_round_dict = {'col1': -1, 'col2': -2} |
3061 |
| - big_df = df * 100 |
3062 |
| - expected_neg_rounded = DataFrame( |
3063 |
| - {'col1':[110., 210, 310], 'col2':[100., 200, 300]}) |
3064 |
| - tm.assert_frame_equal( |
3065 |
| - big_df.round(negative_round_dict), expected_neg_rounded) |
3066 |
| - |
3067 |
| - # nan in Series round |
3068 |
| - nan_round_Series = Series({'col1': nan, 'col2':1}) |
3069 |
| - expected_nan_round = DataFrame( |
3070 |
| - {'col1': [1.123, 2.123, 3.123], 'col2': [1.2, 2.2, 3.2]}) |
3071 |
| - if sys.version < LooseVersion('2.7'): |
3072 |
| - # Rounding with decimal is a ValueError in Python < 2.7 |
3073 |
| - with self.assertRaises(ValueError): |
3074 |
| - df.round(nan_round_Series) |
3075 |
| - else: |
3076 |
| - with self.assertRaises(TypeError): |
3077 |
| - df.round(nan_round_Series) |
3078 |
| - |
3079 |
| - # Make sure this doesn't break existing Series.round |
3080 |
| - tm.assert_series_equal(df['col1'].round(1), expected_rounded['col1']) |
3081 |
| - |
3082 |
| - def test_round_issue(self): |
3083 |
| - # GH11611 |
3084 |
| - |
3085 |
| - df = pd.DataFrame(np.random.random([3, 3]), columns=['A', 'B', 'C'], |
3086 |
| - index=['first', 'second', 'third']) |
3087 |
| - |
3088 |
| - dfs = pd.concat((df, df), axis=1) |
3089 |
| - rounded = dfs.round() |
3090 |
| - self.assertTrue(rounded.index.equals(dfs.index)) |
3091 |
| - |
3092 |
| - decimals = pd.Series([1, 0, 2], index=['A', 'B', 'A']) |
3093 |
| - self.assertRaises(ValueError, df.round, decimals) |
3094 | 2972 |
|
3095 | 2973 | class TestSeriesFormatting(tm.TestCase):
|
3096 | 2974 | _multiprocess_can_split_ = True
|
|
0 commit comments