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
#2
So with some further sleuthing I found this to be the offending code block:

print('start time conversion')
    input()
    #Insert the time only value into backtestdf
    backtestdf['entry_time_only'] = pd.to_datetime(backtestdf['entry_time']).dt.time   
    print('end time conversion')
    input()    
    
That line throws me:

Error:
C:\Users\thpfs\documents\python\cython\test.py:4: 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 import backtest end time conversion
But that is not chained assignment, or is it? Perhaps I don't understand chained assignment all that well if it is... still very strange how this is no problem in the regular python script by the cython compiled script has an issue with this.
Reply
#3
temptimedf = pd.DataFrame()
    print('problem start')
    temptimedf['entry_time_only'] = pd.to_datetime(backtestdf['entry_time']).dt.time
    print('problem end')
    backtestdf['entry_time_only'] = temptimedf['entry_time_only']
    print('now problem')
I tried the above to narrow the problem further....

backtestdf['entry_time_only'] = temptimedf['entry_time_only']

That's the line the cython has a problem with. I still have no idea as to the root cause...
Reply
#4
Hard to tell when you just offer a peek into the code. How is temptimeddf created?
Reply
#5
backtestdf['entry_time_only'] = pd.to_datetime(backtestdf['entry_time']).dt.time
That was the original offensive line of code that triggered the chained assignments warnings.

    temptimedf = pd.DataFrame()
    print('problem start')
    temptimedf['entry_time_only'] = pd.to_datetime(backtestdf['entry_time']).dt.time
    print('problem end')
    backtestdf['entry_time_only'] = temptimedf['entry_time_only']
    print('now problem')
Here you see an attempt that I made to circumvent the issue. In this attempt,
backtestdf['entry_time_only'] = temptimedf['entry_time_only']
is the offending line.

So in backtestdf I have a col called 'entry_time' and this col has datetime objects in YYYY-MM-DD HH:MM:SS format. I need to append a new col to backtest DF that just has the time portion, HH:MM:SS so a time object.

Cython does not like this for some reason.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How is pandas modifying all rows in an assignment - python-newbie question markm74 1 758 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,054 Aug-19-2022, 07:52 AM
Last Post: Gribouillis
  unable to load an image with C (Cython) HLD202 0 1,347 Jan-13-2022, 09:16 PM
Last Post: HLD202
  Cython np.where slows down the code Johanoosterwaal 1 1,782 Sep-01-2021, 07:33 AM
Last Post: Johanoosterwaal
  How can I use Cython to protect my Python Code? TurboC 2 4,212 Dec-03-2020, 10:37 AM
Last Post: padma121
  Why does Cython generates bad code for Gentoo? AlekseyPython 1 1,894 Dec-26-2019, 01:57 PM
Last Post: AlekseyPython
  How I can speed up my Cython module? AlekseyPython 0 1,738 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,843 Jun-05-2019, 09:37 PM
Last Post: Larz60+
  cython does not work skorost5 5 4,338 Dec-19-2018, 09:23 AM
Last Post: skorost5
  Help with building Cython modules. jarrod0987 2 3,374 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