Why use numpy.array() on numpy.arange()? Isn't it redundant?

时间:2017-04-10 00:10:32

标签: python arrays pandas numpy

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 个答案:

答案 0 :(得分:5)

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.