1

I am bit puzzled because I would expect the following code to produce a Pandas DataFrame or Series and instead returns a tuple.

import pandas_datareader.data as web
import random
import pandas as pd

start_date = '2018-01-01'
end_date = '2018-06-06'
SPY = web.DataReader('SPY', 'yahoo', start_date, end_date)
SPY = SPY[['Adj Close']]
SPY.columns = ['Price']

SPY1 = SPY.iloc[random.sample(range(len(SPY.index)), 80), ]
SPY2 = SPY.iloc[random.sample(range(len(SPY.index)), 80), ]

SPY3 = SPY1.align(SPY2, join = 'inner', axis = 0)

type(SPY3)
tuple

I can transform the tuple to a Series as follows:

SPY3 = pd.Series(SPY3[0])

Still I wonder why a tuple is returned in the first place.

halfer
  • 19,824
  • 17
  • 99
  • 186
user8270077
  • 4,621
  • 17
  • 75
  • 140
  • Typical usage would be to reassign the co-aligned objects `SPY1_, SPY2_ = SPY1.align(SPY2, join='inner', axis=0)`. In other words, the first element of the tuple is the aligned version of `SPY1` and the second element of the tuple is the aligned version of `SPY1` – piRSquared Jun 08 '18 at 14:32

1 Answers1

1

The method align returns a tuple according to the documentation:

Returns: (left, right) : (DataFrame, type of other)

Aligned objects

(left, right) is a tuple

jpp
  • 159,742
  • 34
  • 281
  • 339
Stanko
  • 4,275
  • 3
  • 23
  • 51
  • In `SPY1.align(SPY2, join = 'inner', axis = 0)` you say you want to align `SPY1` with `SPY2`. But according to the documentation `SPY2` can be of type Series or a Dataframe. The function align does not only return a Dataframe but also the **type** of `SPY2`. I do not know exactly what the reason is that they return the type. I think it is up to you what you want to do with the type information. – Stanko Jun 08 '18 at 14:11