Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pandas FutureWarning in Table.redrawVisible() #251

Open
AllanHOlesenBW opened this issue Mar 14, 2024 · 2 comments
Open

Pandas FutureWarning in Table.redrawVisible() #251

AllanHOlesenBW opened this issue Mar 14, 2024 · 2 comments

Comments

@AllanHOlesenBW
Copy link

AllanHOlesenBW commented Mar 14, 2024

Excerpt of line 473-476 from Table.redrawVisible() in core.py in pandastable 0.13.1:

        if prec != 0:
            if coldata.dtype == 'float64':
                coldata = coldata.apply(lambda x: self.setPrecision(x, prec), 1)
        coldata = coldata.astype(object).fillna('')

Using pandas 2.2.0, I run into two pandas FutureWarnings, which are repeated hundreds of times when scrolling in the table:

  • In line 475 I get a warning about the convert_dtype parameter being deprecated in the future. This is caused by the second positional argument to coldata.apply()
  • In line 476 I get a warning about downcasting object dtype arrays.

I have tried this change to line 475 and line 476

        if prec != 0:
            if coldata.dtype == 'float64':
                coldata = coldata.apply(lambda x: self.setPrecision(x, prec))
        coldata = coldata.infer_objects(copy=False).fillna('')

This seems to work and does not cause warnings. At least not in pandas 2.2.0.

I could make a pull request on these two changes, but to be honest, both of them feel iffy, so I am not sure it is the right thing to do.

The change in line 475 seems okay. The default for convert_dtype is True, so removing the argument will not change behaviour. When looking in documentation for pandas 0.17, 1.0.0, 1.5.3, 2.0.0 and 2.1.1, this has apparently always been the case. However, I have a nagging feeling that I have seen warnings in earlier versions for not setting this argument.

The change in line 476 is the change proposed by pandas. But as far as I can see in the pandas documentation, the copy keyword to infer_objects() will also be deprecated in the future. So to remove a FutureWarning, pandas suggests a method which already has a futurewarning in the documentation! And to make it worse, this keyword was apparently new in pandas 2.0.0. The 1.5.3 version of infer_objects() does not take any arguments.

These are the two warnings:

Warning caused by line 475. From class SeriesApply.__init__() in apply.py line 1390-1396 in the pandas 2.2.0 package.

       warnings.warn(
            "the convert_dtype parameter is deprecated and will be removed in a "
            "future version.  Do ``ser.astype(object).apply()`` "
            "instead if you want ``convert_dtype=False``.",
            FutureWarning,
            stacklevel=find_stack_level(),
        )

Warning caused by line 476. From class Block._maybe_downcast() in blocks.py line 562-571 in the pandas 2.2.0 package.

                warnings.warn(
                    "Downcasting object dtype arrays on .fillna, .ffill, .bfill "
                    "is deprecated and will change in a future version. "
                    "Call result.infer_objects(copy=False) instead. "
                    "To opt-in to the future "
                    "behavior, set "
                    "`pd.set_option('future.no_silent_downcasting', True)`",
                    FutureWarning,
                    stacklevel=find_stack_level(),
                )
@dmnfarrell
Copy link
Owner

This seems to work. I'll take the chance and change the code. There's plenty of iffy code in there as it is..
It can always be fixed again. thanks.

@scotscotmcc
Copy link

Came here wondering about the same issue and it appears to be addressed in commit c9fc401

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants