gemini-code-assist[bot] commented on code in PR #39056:
URL: https://github.com/apache/beam/pull/39056#discussion_r3454414495
##########
sdks/python/apache_beam/dataframe/frames.py:
##########
@@ -5050,11 +5050,19 @@ def get_dummies(self, **kwargs):
cat.split(sep=kwargs.get('sep', '|')) for cat in dtype.categories
]
+ # Find the name pandas uses for the NaN column in get_dummies().
+ # Older pandas versions (<2.3.0) use 'nan', whereas newer versions
+ # use 'NaN'. We dynamically detect it by running a minimal local operation.
+ dummy_nan_col = pd.Series(
+ ['a', None], dtype='category').str.get_dummies().columns.difference(
+ ['a'])[0]
Review Comment:

Running a dynamic pandas operation (creating a `Series`, calling
`str.get_dummies()`, and computing index differences) on every invocation of
`get_dummies()` introduces unnecessary runtime overhead and could potentially
raise an `IndexError` if the returned columns structure changes in future
pandas versions.
Since the column name used for NaN values is statically determined by the
installed pandas version, we can use a simple and safe version check instead.
This avoids any runtime overhead and is much more robust.
```suggestion
# Find the name pandas uses for the NaN column in get_dummies().
# Older pandas versions (<2.3.0) use 'nan', whereas newer versions
# use 'NaN'.
import re
try:
pd_version = tuple(int(x) for x in re.findall('[0-9]+',
pd.__version__)[0:2])
except Exception:
pd_version = (2, 3)
dummy_nan_col = 'NaN' if pd_version >= (2, 3) else 'nan'
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]