@@ -1264,3 +1264,51 @@ def test_fill_null(df):
1264
1264
)
1265
1265
with pytest .raises (ValueError , match = "Column 'f' not found in DataFrame" ):
1266
1266
df_with_nulls .fill_null ("missing" , subset = ["e" , "f" ])
1267
+
1268
+ def test_fill_nan (df ):
1269
+ # Test filling NaNs with integer value
1270
+ df_with_nans = df .with_column ("d" , literal (float ("nan" )).cast (pa .float64 ()))
1271
+ df_filled = df_with_nans .fill_nan (0 )
1272
+ result = df_filled .to_pydict ()
1273
+ assert result ["d" ] == [0 , 0 , 0 ]
1274
+
1275
+ # Test filling NaNs with float value
1276
+ df_with_nans = df .with_column ("d" , literal (float ("nan" )).cast (pa .float64 ()))
1277
+ df_filled = df_with_nans .fill_nan (99.9 )
1278
+ result = df_filled .to_pydict ()
1279
+ assert result ["d" ] == [99.9 , 99.9 , 99.9 ]
1280
+
1281
+ # Test filling NaNs with subset of columns
1282
+ df_with_nans = df .with_columns (
1283
+ literal (float ("nan" )).cast (pa .float64 ()).alias ("d" ),
1284
+ literal (float ("nan" )).cast (pa .float64 ()).alias ("e" ),
1285
+ )
1286
+ df_filled = df_with_nans .fill_nan (99.9 , subset = ["e" ])
1287
+ result = df_filled .to_pydict ()
1288
+ assert result ["d" ] == [float ("nan" ), float ("nan" ), float ("nan" )]
1289
+ assert result ["e" ] == [99.9 , 99.9 , 99.9 ]
1290
+
1291
+ # Test filling NaNs with value that cannot be cast to column type
1292
+ df_with_nans = df .with_column ("d" , literal (float ("nan" )).cast (pa .float64 ()))
1293
+ with pytest .raises (ValueError , match = "Value must be numeric" ):
1294
+ df_with_nans .fill_nan ("invalid" )
1295
+
1296
+ # Test filling NaNs with subset of columns where some casts fail
1297
+ df_with_nans = df .with_columns (
1298
+ literal (float ("nan" )).alias ("d" ).cast (pa .float64 ()),
1299
+ literal (float ("nan" )).alias ("e" ).cast (pa .float64 ()),
1300
+ )
1301
+ df_filled = df_with_nans .fill_nan (0 , subset = ["d" , "e" ])
1302
+ result = df_filled .to_pydict ()
1303
+ assert result ["d" ] == [0 , 0 , 0 ]
1304
+ assert result ["e" ] == [0 , 0 , 0 ]
1305
+
1306
+ # Test filling NaNs with subset of columns where all casts succeed
1307
+ df_with_nans = df .with_columns (
1308
+ literal (float ("nan" )).alias ("d" ).cast (pa .float64 ()),
1309
+ literal (float ("nan" )).alias ("e" ).cast (pa .float64 ()),
1310
+ )
1311
+ df_filled = df_with_nans .fill_nan (99.9 , subset = ["e" ])
1312
+ result = df_filled .to_pydict ()
1313
+ assert result ["d" ] == [float ("nan" ), float ("nan" ), float ("nan" )]
1314
+ assert result ["e" ] == [99.9 , 99.9 , 99.9 ]
0 commit comments