@@ -2680,6 +2680,115 @@ def test_to_csv_date_format(self):
2680
2680
self .assertEqual (df_day .to_csv (), expected_default_day )
2681
2681
self .assertEqual (df_day .to_csv (date_format = '%Y-%m-%d' ), expected_default_day )
2682
2682
2683
+ def test_round_dataframe (self ):
2684
+
2685
+ # GH 2665
2686
+
2687
+ # Test that rounding an empty DataFrame does nothing
2688
+ df = DataFrame ()
2689
+ tm .assert_frame_equal (df , df .round ())
2690
+
2691
+ # Here's the test frame we'll be working with
2692
+ df = DataFrame (
2693
+ {'col1' : [1.123 , 2.123 , 3.123 ], 'col2' : [1.234 , 2.234 , 3.234 ]})
2694
+
2695
+ # Default round to integer (i.e. decimals=0)
2696
+ expected_rounded = DataFrame (
2697
+ {'col1' : [1. , 2. , 3. ], 'col2' : [1. , 2. , 3. ]})
2698
+ tm .assert_frame_equal (df .round (), expected_rounded )
2699
+
2700
+ # Round with an integer
2701
+ decimals = 2
2702
+ expected_rounded = DataFrame (
2703
+ {'col1' : [1.12 , 2.12 , 3.12 ], 'col2' : [1.23 , 2.23 , 3.23 ]})
2704
+ tm .assert_frame_equal (df .round (decimals ), expected_rounded )
2705
+
2706
+ # This should also work with np.round (since np.round dispatches to
2707
+ # df.round)
2708
+ tm .assert_frame_equal (np .round (df , decimals ), expected_rounded )
2709
+
2710
+ # Round with a list
2711
+ round_list = [1 , 2 ]
2712
+ with self .assertRaises (TypeError ):
2713
+ df .round (round_list )
2714
+
2715
+ # Round with a dictionary
2716
+ expected_rounded = DataFrame (
2717
+ {'col1' : [1.1 , 2.1 , 3.1 ], 'col2' : [1.23 , 2.23 , 3.23 ]})
2718
+ round_dict = {'col1' : 1 , 'col2' : 2 }
2719
+ tm .assert_frame_equal (df .round (round_dict ), expected_rounded )
2720
+
2721
+ # Incomplete dict
2722
+ expected_partially_rounded = DataFrame (
2723
+ {'col1' : [1.123 , 2.123 , 3.123 ], 'col2' : [1.2 , 2.2 , 3.2 ]})
2724
+ partial_round_dict = {'col2' : 1 }
2725
+ tm .assert_frame_equal (
2726
+ df .round (partial_round_dict ), expected_partially_rounded )
2727
+
2728
+ # Dict with unknown elements
2729
+ wrong_round_dict = {'col3' : 2 , 'col2' : 1 }
2730
+ tm .assert_frame_equal (
2731
+ df .round (wrong_round_dict ), expected_partially_rounded )
2732
+
2733
+ # float input to `decimals`
2734
+ non_int_round_dict = {'col1' : 1 , 'col2' : 0.5 }
2735
+ if sys .version < LooseVersion ('2.7' ):
2736
+ # np.round([1.123, 2.123], 0.5) is only a warning in Python 2.6
2737
+ with self .assert_produces_warning (DeprecationWarning ):
2738
+ df .round (non_int_round_dict )
2739
+ else :
2740
+ with self .assertRaises (TypeError ):
2741
+ df .round (non_int_round_dict )
2742
+
2743
+ # String input
2744
+ non_int_round_dict = {'col1' : 1 , 'col2' : 'foo' }
2745
+ with self .assertRaises (TypeError ):
2746
+ df .round (non_int_round_dict )
2747
+
2748
+ non_int_round_Series = Series (non_int_round_dict )
2749
+ with self .assertRaises (TypeError ):
2750
+ df .round (non_int_round_Series )
2751
+
2752
+ # List input
2753
+ non_int_round_dict = {'col1' : 1 , 'col2' : [1 , 2 ]}
2754
+ with self .assertRaises (TypeError ):
2755
+ df .round (non_int_round_dict )
2756
+
2757
+ non_int_round_Series = Series (non_int_round_dict )
2758
+ with self .assertRaises (TypeError ):
2759
+ df .round (non_int_round_Series )
2760
+
2761
+ # Non integer Series inputs
2762
+ non_int_round_Series = Series (non_int_round_dict )
2763
+ with self .assertRaises (TypeError ):
2764
+ df .round (non_int_round_Series )
2765
+
2766
+ non_int_round_Series = Series (non_int_round_dict )
2767
+ with self .assertRaises (TypeError ):
2768
+ df .round (non_int_round_Series )
2769
+
2770
+ # Negative numbers
2771
+ negative_round_dict = {'col1' : - 1 , 'col2' : - 2 }
2772
+ big_df = df * 100
2773
+ expected_neg_rounded = DataFrame (
2774
+ {'col1' :[110. , 210 , 310 ], 'col2' :[100. , 200 , 300 ]})
2775
+ tm .assert_frame_equal (
2776
+ big_df .round (negative_round_dict ), expected_neg_rounded )
2777
+
2778
+ # nan in Series round
2779
+ nan_round_Series = Series ({'col1' : nan , 'col2' :1 })
2780
+ expected_nan_round = DataFrame (
2781
+ {'col1' : [1.123 , 2.123 , 3.123 ], 'col2' : [1.2 , 2.2 , 3.2 ]})
2782
+ if sys .version < LooseVersion ('2.7' ):
2783
+ # Rounding with decimal is a ValueError in Python < 2.7
2784
+ with self .assertRaises (ValueError ):
2785
+ df .round (nan_round_Series )
2786
+ else :
2787
+ with self .assertRaises (TypeError ):
2788
+ df .round (nan_round_Series )
2789
+
2790
+ # Make sure this doesn't break existing Series.round
2791
+ tm .assert_series_equal (df ['col1' ].round (1 ), expected_rounded ['col1' ])
2683
2792
2684
2793
class TestSeriesFormatting (tm .TestCase ):
2685
2794
_multiprocess_can_split_ = True
0 commit comments