Pandas version checks

  • [X] I have checked that this issue has not already been reported.

  • [X] I have confirmed this bug exists on the latest version of pandas.

  • [X] I have confirmed this bug exists on the main branch of pandas.

Reproducible Example

with pd.option_context('future.no_silent_downcasting', True):
  pd.Series([None]).fillna(False)

Issue Description

I was not able to find a code that doesn't throw warnings for pandas >= 2.0. The example will work in pandas 2.2, but will break for earlier versions. Ahem... This is probably the first time in my Python development that a backwards incompatible change has been issued with a backwards incompatible workaround.

Expected Behavior

Not throw any warning.

i could do something like this

if pandas.__version__ >= '2.2.0':
  with pd.option_context('future.no_silent_downcasting', True):
    pd.Series([None]).fillna(False)
else:
  pd.Series([None]).fillna(False)

I suppose this code would work, but it's a bad code.

Installed Versions

pandas : 2.2.2 numpy : 1.26.3 pytz : 2024.1 dateutil : 2.8.1

Comment From: rhshadrach

Thanks for the report.

This is probably the first time in my Python development that a backwards incompatible change has been issued with a backwards incompatible workaround.

future.no_silent_downcasting was introduced so that users could adopt the future behavior of the breaking change, not to be a backwards compatible way to avoid the warning.

I suppose this code would work, but it's a bad code.

Why is it bad code? If you'd like to support multiple versions of pandas, I think you'll need to branch on the version. Though it is probably better practice to access the version via from importlib.metadata import version and not rely on string comparisons (e.g. https://packaging.pypa.io/en/latest/version.html).