advanced patterns with DataFrame.fillna() on Mixed Data Types in Pandas 1.5.3
Does anyone know how to I'm having a hard time understanding I'm working with an scenario when using the `fillna()` method on a DataFrame with mixed data types in Pandas version 1.5.3. My DataFrame contains both numeric and string columns, and I want to fill NaN values in the numeric columns with the mean of those columns while replacing NaN in string columns with a placeholder text. Hereβs a simplified version of my DataFrame: ```python import pandas as pd import numpy as np data = { 'A': [1, 2, np.nan, 4], 'B': ['foo', np.nan, 'bar', 'baz'], 'C': [np.nan, 1.5, 2.5, 3.5] } df = pd.DataFrame(data) ``` When I try to apply `fillna()` like this: ```python mean_A = df['A'].mean() # Fill NaN in numeric columns numeric_cols = df.select_dtypes(include=[np.number]).columns # I expect this to only affect the numeric columns for col in numeric_cols: df[col].fillna(mean_A, inplace=True) # Now, fill string columns string_cols = df.select_dtypes(include=[object]).columns for col in string_cols: df[col].fillna('missing', inplace=True) ``` I expect that after running this code, NaN values in column 'A' should be replaced with the mean, and NaN in column 'B' should be replaced with 'missing'. However, when I check the DataFrame, I notice that the numeric columns are filled correctly, but column 'B' seems to remain unchanged. The output looks like this: ``` A B C 0 1.0 foo NaN 1 2.0 NaN 1.5 2 2.5 bar 2.5 3 4.0 baz 3.5 ``` I'm not seeing any errors, but the NaN in column 'B' isn't being replaced as expected. Iβve tried changing the method to `df[col].fillna('missing', inplace=True)` directly, but it doesn't help. Am I missing something in the way I'm applying `fillna()`? Is there a more efficient approach I should consider for this scenario? This is part of a larger REST API I'm building. This issue appeared after updating to Python LTS. I'd really appreciate any guidance on this. Has anyone dealt with something similar?