@@ -267,7 +267,7 @@ def test_sub_datetimelike_align(self):
267
267
268
268
def test_alignment_doesnt_change_tz (self ):
269
269
# GH#33671
270
- dti = pd . date_range ("2016-01-01" , periods = 10 , tz = "CET" )
270
+ dti = date_range ("2016-01-01" , periods = 10 , tz = "CET" )
271
271
dti_utc = dti .tz_convert ("UTC" )
272
272
ser = Series (10 , index = dti )
273
273
ser_utc = Series (10 , index = dti_utc )
@@ -399,7 +399,7 @@ def test_ser_flex_cmp_return_dtypes_empty(self, opname):
399
399
)
400
400
def test_ser_cmp_result_names (self , names , op ):
401
401
# datetime64 dtype
402
- dti = pd . date_range ("1949-06-07 03:00:00" , freq = "H" , periods = 5 , name = names [0 ])
402
+ dti = date_range ("1949-06-07 03:00:00" , freq = "H" , periods = 5 , name = names [0 ])
403
403
ser = Series (dti ).rename (names [1 ])
404
404
result = op (ser , dti )
405
405
assert result .name == names [2 ]
@@ -624,9 +624,13 @@ def test_ne(self):
624
624
),
625
625
],
626
626
)
627
- def test_comp_ops_df_compat (self , left , right ):
627
+ def test_comp_ops_df_compat (self , left , right , frame_or_series ):
628
628
# GH 1134
629
- msg = "Can only compare identically-labeled Series objects"
629
+ msg = f"Can only compare identically-labeled { frame_or_series .__name__ } objects"
630
+ if frame_or_series is not Series :
631
+ left = left .to_frame ()
632
+ right = right .to_frame ()
633
+
630
634
with pytest .raises (ValueError , match = msg ):
631
635
left == right
632
636
with pytest .raises (ValueError , match = msg ):
@@ -642,22 +646,6 @@ def test_comp_ops_df_compat(self, left, right):
642
646
with pytest .raises (ValueError , match = msg ):
643
647
right < left
644
648
645
- msg = "Can only compare identically-labeled DataFrame objects"
646
- with pytest .raises (ValueError , match = msg ):
647
- left .to_frame () == right .to_frame ()
648
- with pytest .raises (ValueError , match = msg ):
649
- right .to_frame () == left .to_frame ()
650
-
651
- with pytest .raises (ValueError , match = msg ):
652
- left .to_frame () != right .to_frame ()
653
- with pytest .raises (ValueError , match = msg ):
654
- right .to_frame () != left .to_frame ()
655
-
656
- with pytest .raises (ValueError , match = msg ):
657
- left .to_frame () < right .to_frame ()
658
- with pytest .raises (ValueError , match = msg ):
659
- right .to_frame () < left .to_frame ()
660
-
661
649
def test_compare_series_interval_keyword (self ):
662
650
# GH#25338
663
651
s = Series (["IntervalA" , "IntervalB" , "IntervalC" ])
@@ -724,7 +712,7 @@ def test_series_add_aware_naive_raises(self):
724
712
def test_datetime_understood (self ):
725
713
# Ensures it doesn't fail to create the right series
726
714
# reported in issue#16726
727
- series = Series (pd . date_range ("2012-01-01" , periods = 3 ))
715
+ series = Series (date_range ("2012-01-01" , periods = 3 ))
728
716
offset = pd .offsets .DateOffset (days = 6 )
729
717
result = series - offset
730
718
expected = Series (pd .to_datetime (["2011-12-26" , "2011-12-27" , "2011-12-28" ]))
@@ -746,58 +734,46 @@ def test_align_date_objects_with_datetimeindex(self):
746
734
tm .assert_series_equal (result2 , expected )
747
735
748
736
749
- @pytest .mark .parametrize (
750
- "names" ,
751
- [
752
- ("foo" , None , None ),
753
- ("Egon" , "Venkman" , None ),
754
- ("NCC1701D" , "NCC1701D" , "NCC1701D" ),
755
- ],
756
- )
757
- @pytest .mark .parametrize ("box" , [list , tuple , np .array , pd .Index , pd .Series , pd .array ])
758
- @pytest .mark .parametrize ("flex" , [True , False ])
759
- def test_series_ops_name_retention (flex , box , names , all_binary_operators , request ):
760
- # GH#33930 consistent name retention
761
- op = all_binary_operators
762
-
763
- if op is ops .rfloordiv and box in [list , tuple ] and not flex :
764
- mark = pytest .mark .xfail (
765
- reason = "op fails because of inconsistent ndarray-wrapping GH#28759"
766
- )
767
- request .node .add_marker (mark )
768
-
769
- left = Series (range (10 ), name = names [0 ])
770
- right = Series (range (10 ), name = names [1 ])
771
-
772
- name = op .__name__ .strip ("_" )
773
- is_logical = name in ["and" , "rand" , "xor" , "rxor" , "or" , "ror" ]
774
- is_rlogical = is_logical and name .startswith ("r" )
775
-
776
- right = box (right )
777
- if flex :
778
- if is_logical :
779
- # Series doesn't have these as flex methods
737
+ class TestNamePreservation :
738
+ @pytest .mark .parametrize ("box" , [list , tuple , np .array , Index , Series , pd .array ])
739
+ @pytest .mark .parametrize ("flex" , [True , False ])
740
+ def test_series_ops_name_retention (self , flex , box , names , all_binary_operators ):
741
+ # GH#33930 consistent name renteiton
742
+ op = all_binary_operators
743
+
744
+ if op is ops .rfloordiv and box in [list , tuple ]:
745
+ pytest .xfail ("op fails because of inconsistent ndarray-wrapping GH#28759" )
746
+
747
+ left = Series (range (10 ), name = names [0 ])
748
+ right = Series (range (10 ), name = names [1 ])
749
+
750
+ name = op .__name__ .strip ("_" )
751
+ is_logical = name in ["and" , "rand" , "xor" , "rxor" , "or" , "ror" ]
752
+ is_rlogical = is_logical and name .startswith ("r" )
753
+
754
+ right = box (right )
755
+ if flex :
756
+ if is_logical :
757
+ # Series doesn't have these as flex methods
758
+ return
759
+ result = getattr (left , name )(right )
760
+ else :
761
+ # GH#37374 logical ops behaving as set ops deprecated
762
+ warn = FutureWarning if is_rlogical and box is Index else None
763
+ with tm .assert_produces_warning (warn , check_stacklevel = False ):
764
+ result = op (left , right )
765
+
766
+ if box is Index and is_rlogical :
767
+ # Index treats these as set operators, so does not defer
768
+ assert isinstance (result , Index )
780
769
return
781
- result = getattr (left , name )(right )
782
- else :
783
- # GH#37374 logical ops behaving as set ops deprecated
784
- warn = FutureWarning if is_rlogical and box is Index else None
785
- with tm .assert_produces_warning (warn , check_stacklevel = False ):
786
- result = op (left , right )
787
-
788
- if box is pd .Index and is_rlogical :
789
- # Index treats these as set operators, so does not defer
790
- assert isinstance (result , pd .Index )
791
- return
792
-
793
- assert isinstance (result , Series )
794
- if box in [pd .Index , pd .Series ]:
795
- assert result .name == names [2 ]
796
- else :
797
- assert result .name == names [0 ]
798
770
771
+ assert isinstance (result , Series )
772
+ if box in [Index , Series ]:
773
+ assert result .name == names [2 ]
774
+ else :
775
+ assert result .name == names [0 ]
799
776
800
- class TestNamePreservation :
801
777
def test_binop_maybe_preserve_name (self , datetime_series ):
802
778
# names match, preserve
803
779
result = datetime_series * datetime_series
0 commit comments