19

My output:

img

def load_data(self):
    """
    Load data from list of paths
    :return: 3D-array X and 2D-array y
    """
    X = None
    y = None
    df = pd.read_excel('data/Data.xlsx', header=None)
    for i in range(len(df.columns)):
        sentences_ = df[i].to_numpy().tolist()
        label_vec = [0.0 for _ in range(0, self.n_class)]
        label_vec[i] = 1.0
        labels_ = [label_vec for _ in range(0, len(sentences_))]
        if X is None:
            X = sentences_
            y = labels_
        else:
            X += sentences_
            y += labels_
    X, max_length = self.tokenize_sentences(X)
    X = self.word_embed_sentences(X, max_length=self.max_length)
    return np.array(X), np.array(y)

This is my code with pandas library as pd. When I run in Google Colab I get the following error:

AttributeError: 'Series' object has no attribute 'to_numpy'
6
  • Please add the error output for more details, like at which line does the error occurs ? Commented Feb 12, 2019 at 13:05
  • What pandas version are you using? You could try df[i].values.tolist() ? Commented Feb 12, 2019 at 13:08
  • Eror in sentences_ = df[i].to_numpy().tolist() Commented Feb 12, 2019 at 13:10
  • I use pandas 0.24.1 Commented Feb 12, 2019 at 13:11
  • 4
    to_numpy() is a new method, I'd double check your Pandas version in the colab - maybe it doesn't match Commented Feb 12, 2019 at 13:21

2 Answers 2

27

Check the version of your pandas library:

import pandas
print(pandas.__version__)

If your version is less than 0.24.1:

pip install --upgrade pandas
Sign up to request clarification or add additional context in comments.

Comments

19

If you need your code to work with all versions of pandas, here's a simple way to convert a Series into a NumPy array:

import pandas as pd
import numpy as np

s = pd.Series([1.1, 2.3])
a = np.array(s)
print(a)  # [1.1 2.3]

On an advanced note, if your Series has missing values (as NaN values), these can be converted to a masked array:

s = pd.Series([1.1, np.nan])
a = np.ma.masked_invalid(s)
print(a)  # [1.1 --]

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.