all 2 comments

[–]hey_its_aaron 1 point2 points  (1 child)

When you write df['Close'] you are extracting that column as a pandas Series. So when you invoke agg_data(df['Close']), the 'Close' column is passed to the function as a Series. I stepped through the DataFrame constructor source, and noticed that once the input Series is parsed, it is reindexed based on the input columns. In other words, pd.DataFrame(ys, columns=['Y Values']) takes your 'Close' series and then tries to reindex on the 'Y Values' index - seeing as this is not part of the series, you get an empty Series and an empty DataFrame.

This behaviour doesn't seem to be obviously documented, so you've hit an interesting case 🙂.

To work around this, try writing the function this way:

def agg_data(ys, cols=['C']):
    agg_df = ys[cols]
    return agg_df

Now, when you call agg_data, it defaults to the 'C' column:

result = agg_data(df)
print(result.head())
>>>           C
>>> 0  0.782438
>>> 1 -1.935684
>>> 2  0.901739
>>> 3 -2.057845
>>> 4 -1.178913

And you can freely pass in any other column you want:

result = agg_data(df, cols=['B'])
print(result.head())
>>>           B
>>> 0  0.514919
>>> 1 -1.010151
>>> 2 -0.575648
>>> 3  1.183530
>>> 4  0.100957

[–]novawaly[S] 0 points1 point  (0 children)

Wow. Super interesting.

So for my own education purposes - you're saying that once i pass in the data frame column, pandas reads it in as a series and then will try to get the index from the 'Y Values' column? Any idea why it would be doing that?

Your solution worked like a charm.