'Getting AttributeError: __enter__ when trying to print pandas dataframe with pd.set_option?

I have this code:

if len(self.my_df)==865:
    with pd.set_option("display.max_rows", None, "display.max_columns", None):
        print(self.my_df)

According to Pretty-print an entire Pandas Series / DataFrame, that should be the correct syntax - and yet that line crashes with:

  File "myfile.py", line 1132, in my_function
    with pd.set_option("display.max_rows", None, "display.max_columns", None):
AttributeError: __enter__

Why is this happening?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source