5

In my Numpy / Pandas lesson I am taught to make data with the following code:

sample_numpy_data = np.array(np.arange(24)).reshape((6,4))

Isn't it redundant to use np.array() on the np.arange() as np.arange() already produces an array?

Why is this necessary? example?

Example found in Lynda course "Pandas for Data Science" Pandas Overview - Operations.

1
  • 7
    Yup. That is redundant. Commented Apr 10, 2017 at 0:11

1 Answer 1

7

It is not only redundant, it also introduces unnecessary overhead because it makes a copy of the array by default.

If you're not sure if something is an array (maybe because it's an argument for a function) you can use np.asarray on it. That only copies if it's not an array.

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

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.