0
$\begingroup$

This is my code.

import pandas as pd
import pandas_datareader.data as web
import datetime as dt

start = dt.datetime(2019, 2, 1)
end = datetime.today().strftime('%Y-%m-%d')
slug = "tsla"
df = web.DataReader(slug, 'yahoo', start, end)

print(df)

This prints this --->

                  High         Low        Open       Close      Volume   Adj Close
Date
2019-01-31   62.312000   58.799999   60.200001   61.403999  62846000.0   61.403999
2019-02-01   63.220001   60.700001   61.084000   62.442001  36417000.0   62.442001
2019-02-04   63.060001   60.375999   62.596001   62.577999  36760500.0   62.577999
2019-02-05   64.487999   62.450001   62.498001   64.269997  33714000.0   64.269997
2019-02-06   64.848000   63.124001   63.917999   63.444000  25192500.0   63.444000
...                ...         ...         ...         ...         ...         ...
2020-10-12  448.739990  438.579987  442.000000  442.299988  38791100.0  442.299988
2020-10-13  448.890015  436.600006  443.350006  446.649994  34463700.0  446.649994
2020-10-14  465.899994  447.350006  449.779999  461.299988  48045400.0  461.299988
2020-10-15  456.570007  442.500000  450.309998  448.880005  35672400.0  448.880005
2020-10-16  455.950012  438.850006  454.440002  439.670013  32620000.0  439.670013

[433 rows x 6 columns]

I only want to get this Date column from df, If i write

df['Date']

it gives me error, but if i use CSV file, then everything works fine

df.to_csv('%s.csv' % slug)
df = pd.read_csv('%s.csv' % slug)

but it creates so much csv files for every company, so i want to use it without CSV

$\endgroup$
3
  • $\begingroup$ fig = go.Figure(data=[go.Candlestick(x=df['Date'], open=df['Open'], high=df['High'], low=df['Low'], close=df['Close'], line=dict(width=1))]) $\endgroup$ Oct 19 '20 at 7:06
  • $\begingroup$ I want to give this date attribute to candlesticks but without use of memory $\endgroup$ Oct 19 '20 at 7:07
  • $\begingroup$ Please do not use the comments space for adding information or clarifications, especially if they contain code - edit & update your post instead. $\endgroup$
    – desertnaut
    Oct 19 '20 at 8:08
1
$\begingroup$

You need to reset index using df.reset_index().This will make your Dates attribute/column and not an index as it is now.

import pandas as pd
import pandas_datareader.data as web
import datetime as dt

start = dt.datetime(2019, 2, 1)
end = dt.datetime.today().strftime('%Y-%m-%d')
slug = "tsla"
df = web.DataReader(slug, 'yahoo', start, end)
modified = df.reset_index()
print(modified['Date'])
$\endgroup$
1
  • $\begingroup$ this solved the same issue for me, thanks. $\endgroup$
    – Loran
    Dec 19 '21 at 8:08

Not the answer you're looking for? Browse other questions tagged or ask your own question.