File tree 3 files changed +23
-7
lines changed
third_party/bigframes_vendored/pandas/core
3 files changed +23
-7
lines changed Original file line number Diff line number Diff line change @@ -192,7 +192,15 @@ def __setitem__(
192
192
and isinstance (key [0 ], bigframes .series .Series )
193
193
and key [0 ].dtype == "boolean"
194
194
) and pd .api .types .is_scalar (value ):
195
- new_column = key [0 ].map ({True : value , False : None })
195
+ # For integer scalar, if set value to a new column, the dtype would be default to float.
196
+ # But if set value to an existing Int64 column, the dtype would still be integer.
197
+ # So we need to use different NaN type to match this behavior.
198
+ new_column = key [0 ].map (
199
+ {
200
+ True : value ,
201
+ False : pd .NA if key [1 ] in self ._dataframe .columns else None ,
202
+ }
203
+ )
196
204
try :
197
205
original_column = self ._dataframe [key [1 ]]
198
206
except KeyError :
Original file line number Diff line number Diff line change @@ -2918,15 +2918,23 @@ def test_loc_setitem_bool_series_scalar_new_col(scalars_dfs):
2918
2918
)
2919
2919
2920
2920
2921
- def test_loc_setitem_bool_series_scalar_existing_col (scalars_dfs ):
2921
+ @pytest .mark .parametrize (
2922
+ ("col" , "value" ),
2923
+ [
2924
+ ("string_col" , "hello" ),
2925
+ ("int64_col" , 3 ),
2926
+ ("float64_col" , 3.5 ),
2927
+ ],
2928
+ )
2929
+ def test_loc_setitem_bool_series_scalar_existing_col (scalars_dfs , col , value ):
2922
2930
if pd .__version__ .startswith ("1." ):
2923
2931
pytest .skip ("this loc overload not supported in pandas 1.x." )
2924
2932
2925
2933
scalars_df , scalars_pandas_df = scalars_dfs
2926
2934
bf_df = scalars_df .copy ()
2927
2935
pd_df = scalars_pandas_df .copy ()
2928
- bf_df .loc [bf_df ["int64_too" ] == 1 , "string_col" ] = "hello"
2929
- pd_df .loc [pd_df ["int64_too" ] == 1 , "string_col" ] = "hello"
2936
+ bf_df .loc [bf_df ["int64_too" ] == 1 , col ] = value
2937
+ pd_df .loc [pd_df ["int64_too" ] == 1 , col ] = value
2930
2938
2931
2939
pd .testing .assert_frame_equal (
2932
2940
bf_df .to_pandas (),
Original file line number Diff line number Diff line change @@ -662,9 +662,9 @@ def copy(self):
662
662
663
663
>>> df.loc[df["b"] == 2, "b"] = 22
664
664
>>> df
665
- a b
666
- 0 1 22.0
667
- 1 3 4.0
665
+ a b
666
+ 0 1 22
667
+ 1 3 4
668
668
<BLANKLINE>
669
669
[2 rows x 2 columns]
670
670
>>> df_copy
You can’t perform that action at this time.
0 commit comments