In python, get inconsistent returns when trying to reverse Pandas DataFrame values

Question:

I am trying to reverse values of DataFrame in Python as in the following codes. Strangely, x retuns what expected, but not y.

import pandas as pd
import pandas_datareader as pdr

x = pdr.DataReader('TQQQ', data_source='yahoo', start='2000-01-01')  
y = pdr.DataReader('^NDX', data_source='yahoo', start='2000-01-01') # ^NDX is a symbol for Nasdaq 100 Index

x[:] = x[::-1]
y[:] = y[::-1]

print(x)
print(y)

I suppose which symbol is called should not be a matter, but it seems to be matter.

Please help with it. Thank in advance for any comment and advice.

Asked By: david78

||

Answers:

x[::-1] # reverses the data frame along with index i.e., date  
x[:]=x[::-1] # This replaces the data frame values but does not change index.  

Therefore you can do this

x=x[::-1]
Answered By: Srujana

I feel there’s inconsistency in the data downloaded. If you change all the columns as float type or string type, the 2 dataframes have the same behavior (doing nothing), as expected.

import pandas_datareader as pdr

x = pdr.DataReader('TQQQ', data_source='yahoo', start='2000-01-01')  
print(x)
for col in x.columns:
    x[col] = x[col].astype('float')
    #x[col] = x[col].astype('string')
x[:] = x[::-1]
print(x)

y = pdr.DataReader('^NDX', data_source='yahoo', start='2000-01-01') # ^NDX is a symbol for Nasdaq 100 Index
print(y)
for col in x.columns:
    y[col] = y[col].astype('float')
    #y[col] = y[col].astype('string')
y[:] = y[::-1]
print(y)

I have tried this line df[:] = df[::-1] on other dataframe, and it does not change anything.
However, this line df = df[::-1] will reverse the dataframe.

You should be using these lines instead:

x = x[::-1]
y = y[::-1]

Alternatively, you could use sort_values() to reverse the dataframe:

x = x.sort_values('Date', ascending=False)
y = y.sort_values('Date', ascending=False)
Answered By: perpetualstudent
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.