What will be the correct sequence of the following statements to create a series from a one-dimensional (1D) NumPy array?
(A) >>> import pandas as pd
(B) >>> s = pd.Series(a)
(C) >>> a = np.array([1,2,3,4])
(D) >>> import numpy as np
What will be the correct sequence of the following statements to create a series from a one-dimensional (1D) NumPy array?
(A) >>> import pandas as pd
(B) >>> s = pd.Series(a)
(C) >>> a = np.array([1,2,3,4])
(D) >>> import numpy as np
Solution
np must be imported before np.array() can be used, and a must exist before it is passed to pd.Series(), which in turn needs pandas imported as pd. So (D) comes before (C), and (C) and (A) both come before (B).
(D) >>> import numpy as np
(C) >>> a = np.array([1,2,3,4])
(A) >>> import pandas as pd
(B) >>> s = pd.Series(a)
Options 1 and 4: (C) uses np before numpy is imported in (D), which gives a NameError.
Option 2: (B) uses pd and a before either is defined.
Related questions:
2025: 27 May Shift 2
2026: 23 May Shift 2
2026: 23 May Shift 2