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

TypeError: 'rx' object does not support item assignment #965

Open
ahuang11 opened this issue Aug 28, 2024 · 3 comments
Open

TypeError: 'rx' object does not support item assignment #965

ahuang11 opened this issue Aug 28, 2024 · 3 comments
Labels
TRIAGE User-submitted issue that hasn't been triaged yet. type-bug Bug report

Comments

@ahuang11
Copy link
Contributor

ahuang11 commented Aug 28, 2024

Originally from
https://discourse.holoviz.org/t/normalizing-stock-market-data-with-reactive-dataframe/7515


I am trying to normalize stock market data of an reactive DataFrame. I am getting an error and need a bit of help.

This is the minimal, reproducible example:

import pandas as pd
import panel as pn

# Create DateTimeIndex
dates = pd.date_range(start='2023-01-01', end='2023-01-10', freq='D')

# Price data
prices = [101.5, 98.2, 103.7, 100.4, 104.6, 97.8, 106.3, 99.5, 105.2, 108.0]

# Create DataFrame
df = pd.DataFrame(data={'Price': prices}, index=dates)

In a normal DataFrame I would do this to normalize the stock price:

# First Index
first_index = df.index[0]

# Normalize Price
df["Price_normalized"] = df["Price"] / df.loc[first_index, 'Price']

print(df)

However, in my case I am filtering the DataFrame with a DateRangeSlider. Because of this the first_index, on which i normalize, changes. It should always be the first price of the filtered DataFrame "rdf". The basic pandas method does not work and I get an error.

rdf=pn.rx(df)

# First Index
first_index = rdf.index[0]

# Normalize Price
rdf["Price_normalized"] = rdf["Price"] / rdf.loc[first_index, 'Price']

I am getting the TypeError:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[33], line 7
      4 first_index = rdf.index[0]
      6 # Normalize Price
----> 7 rdf["Price_normalized"] = rdf["Price"] / rdf.loc[first_index, 'Price']

TypeError: 'rx' object does not support item assignment
@ahuang11 ahuang11 added type-bug Bug report TRIAGE User-submitted issue that hasn't been triaged yet. labels Aug 28, 2024
@philippjfr
Copy link
Member

Can you use assign?

rdf.assign(Price_normalized=rdf["Price"] / rdf.loc[first_index, 'Price']})

@ahuang11
Copy link
Contributor Author

ahuang11 commented Aug 28, 2024

Yeah I think that works.

import pandas as pd
import panel as pn
pn.extension()

# Create DateTimeIndex
dates = pd.date_range(start='2023-01-01', end='2023-01-10', freq='D')

# Price data
prices = [101.5, 98.2, 103.7, 100.4, 104.6, 97.8, 106.3, 99.5, 105.2, 108.0]

# Create DataFrame
df = pd.DataFrame(data={'Price': prices, 'Dates': dates})
rdf = pn.rx(df)
first_index = rdf.index[0]
rdf.assign(Price_normalized=rdf["Price"] / rdf.loc[first_index, 'Price'])

@philippjfr
Copy link
Member

Method chaining should generally be preferred over assignment, but we might still consider __setitem__.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
TRIAGE User-submitted issue that hasn't been triaged yet. type-bug Bug report
Projects
None yet
Development

No branches or pull requests

2 participants