0

Me again... :)

I tried finding an answer to this question but again I was not fortunate enough. So here it is.

What is the difference between calling a numpy array (let's say "iris") and the whole group of data in this array (by using iris[:] for instance).

I´m asking this because of the error that I get when I run the first example (below), while the second example works fine.

Here is the code:

At this first part I load the library and import the dataset from the internet.

import statsmodels.api as sm
iris = sm.datasets.get_rdataset(dataname='iris',
                            package='datasets')['data']

If I run this code I get an error:

iris.columns.values = [iris.columns.values[x].lower() for x in range( len( iris.columns.values ) ) ]
print(iris.columns.values)

Now if I run this code it works fine:

iris.columns.values[:] = [iris.columns.values[x].lower() for x in range( len( iris.columns.values ) ) ]
print(iris.columns.values)

Best regards,

8
  • Before you attempt that assignment, what is iris.columns.values? I.e. type? Maybe also check iris.columns. And what is sm, as in the sm.datasets? Commented Mar 8, 2019 at 0:33
  • The reason I want you to be specific about those types is that I don't think iris.columns.values is an ordinary ndarray. It may be a property of iris.columns. As such it could be accessed (get), modified (with the [:]= syntax), but not set. Commented Mar 8, 2019 at 0:49
  • The values were: 'Sepal.Length', 'Sepal.Width', 'Petal.Length', 'Petal.Width', 'Species' and they were stored in: iris.columns.values. Its type is a numpy.ndarray. And sm is statsmodel.api package. I copied the wrong import command. I should have printed: import statsmodels.api as sm. I will try editing my question to reflect that. Thanks for pointing me this. Commented Mar 8, 2019 at 0:50
  • By the way I runned into this kind of problem but with iris.keys() and with iris.columns. They are the ones that cannot be modified since they are indexes. I checked here in stackoverflow for this and the solution presented was to change iris.columns.values instead. It worked, but I'd say it worked "too much" since it is changing also something that was not supposed to be changed. Commented Mar 8, 2019 at 0:56
  • Keeping on the topic that you raised, what is the difference between modifying a value and setting it? Sorry if this is a naive question, but I'm a hookie in Python language. Commented Mar 8, 2019 at 0:58

2 Answers 2

2

The difference is that when you do iris.columns.values = ... you try to replace the reference of the values property in iris.columns which is protected (see pandas implementation of pandas.core.frame.DataFrame) and when you do iris.columns.values[:] = ... you access the data of the np.ndarray and replace it with new values. In the second assignment statement you do not overwrite the reference to the numpy object. The [:] is a slice object that is passed to the __setitem__ method of the numpy array.

EDIT:

The exact implementation (there are multiple, here is the pd.Series implementation) of such property is:

    @property
    def values(self):
        """ return the array """
        return self.block.values

thus you try to overwrite a property that is constructed with a decorator @property followed by a getter function, and cannot be replaced since it is only provided with a getter and not a setter. See Python's docs on builtins - property()

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! Again, the object and its values... And there goes up the learning curve in Python... Best regards mr_mo!
Have fun! added some supplementary materials :)
1
iris.columns.values = val

calls

type(iris.columns).__setattr__(iris.columns, 'values', val)

This is running pandas' code, because type(iris.columns) is pd.Series


iris.columns.values[:] = val

calls

type(iris.columns.value).__setitem__(iris.columns.value, slice(None), val)

This is running numpy's code, because type(iris.columns.value) is np.ndarray

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.