Python Forum
Cython, Pandas, and Chained Assignment
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Cython, Pandas, and Chained Assignment
#1
So have a script called backtest.pyx. Script works fine with no errors/warnings.

Now I compliled the entire script using cython and I have the pyd file backtest.cp312-win_amd64.pyd

Next I have a script called test.py where the code in there is just:

import backtest
So I run test.py, and the cython compiled version of my script runs. FYI I did not make any cython specific optimizations to my backtest.pyx script, I'm just compiling the script as-is for nowsince this is the start of my cython journey.

When the compiled version of the script runs, I get a bunch of warnings:

Error:
C:\Users\thpfs\documents\python\cython\test.py:1: FutureWarning: ChainedAssignmentError: behaviour will change in pandas 3.0! You are setting values through chained assignment. Currently this works in certain cases, but when using Copy-on-Write (which will become the default behaviour in pandas 3.0) this will never work to update the original DataFrame or Series, because the intermediate object on which we are setting values will behave as a copy. A typical example is when you are setting values in a column of a DataFrame, like: df["col"][row_indexer] = value Use `df.loc[row_indexer, "col"] = values` instead, to perform the assignment in a single step and ensure this keeps updating the original `df`. See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
So I want to fix the chained assignments, so in my original, uncompiled script backtest.pyx I add

pd.set_option('mode.chained_assignment', 'raise')
This script still runs fine, no warnings/errors raised. I've scanned my code, I can't find any chained assingments. But cython compiled still throws warnings for chained assignments...

Now I suppose I could just try and silence those warnings. But I'd rather fix the core issue. I do need to do something because I need to run my script in parallel using joblib - right now the cython compiled version just gets hung when I'm using the joblib code, I'm guessing that may be because of the warnings thrown. All of the above examples was me using a for loop to run a function in series instead of using joblib to run in parallel.

What is the solution?
Reply


Messages In This Thread
Cython, Pandas, and Chained Assignment - by sawtooth500 - Apr-12-2024, 09:32 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How is pandas modifying all rows in an assignment - python-newbie question markm74 1 819 Nov-28-2023, 10:36 PM
Last Post: deanhystad
  I need to add data types to cython conversion python to c Good_AI_User 1 1,110 Aug-19-2022, 07:52 AM
Last Post: Gribouillis
  unable to load an image with C (Cython) HLD202 0 1,376 Jan-13-2022, 09:16 PM
Last Post: HLD202
  Cython np.where slows down the code Johanoosterwaal 1 1,829 Sep-01-2021, 07:33 AM
Last Post: Johanoosterwaal
  How can I use Cython to protect my Python Code? TurboC 2 4,299 Dec-03-2020, 10:37 AM
Last Post: padma121
  Why does Cython generates bad code for Gentoo? AlekseyPython 1 1,929 Dec-26-2019, 01:57 PM
Last Post: AlekseyPython
  How I can speed up my Cython module? AlekseyPython 0 1,768 Oct-26-2019, 01:23 PM
Last Post: AlekseyPython
  Error in compiling cython extension on Python 3.6.4 on Windows 8 goldenmean 3 5,917 Jun-05-2019, 09:37 PM
Last Post: Larz60+
  cython does not work skorost5 5 4,432 Dec-19-2018, 09:23 AM
Last Post: skorost5
  Help with building Cython modules. jarrod0987 2 3,436 Nov-15-2018, 03:57 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020