Error while importing data from yfinance

I recently started my learning track 1 on Quantra. Currently learning how to import data from various sources. I am facing issue while importing data from yfinance. request the team to help me understand how to resolve this error. I am a novice python learner and started this course a week back only.



Sharing the codes of the error:



import yfinance as yf

data = yf.download('AAPL', start="2022-01-01", end="2023-04-30")

data.head()

 

[*********************100%%**********************]  1 of 1 completed

1 Failed download:
['AAPL']: ImportError("cannot import name 'find_stack_level' from 'pandas.util._exceptions' (C:\\Users\\Mayuresh\\.conda\\envs\\quantra_py\\lib\\site-packages\\pandas\\util\\_exceptions.py)")
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
File ~\.conda\envs\quantra_py\lib\site-packages\IPython\core\formatters.py:344, in BaseFormatter.__call__(self, obj)
    342     method = get_real_method(obj, self.print_method)
    343     if method is not None:
--> 344         return method()
    345     return None
    346 else:

File ~\.conda\envs\quantra_py\lib\site-packages\pandas\core\frame.py:855, in _repr_html_(self)
    840         arrays, columns, index = nested_data_to_arrays(
    841             # error: Argument 3 to "nested_data_to_arrays" has incompatible
    842             # type "Optional[Collection[Any]]"; expected "Optional[Index]"
   (...)
    846             dtype,
    847         )
    848         mgr = arrays_to_mgr(
    849             arrays,
    850             columns,
   (...)
    853             typ=manager,
    854         )
--> 855     else:
    856         mgr = ndarray_to_mgr(
    857             data,
    858             index,
   (...)
    862             typ=manager,
    863         )
    864 else:

File ~\.conda\envs\quantra_py\lib\site-packages\pandas\io\formats\format.py:1007, in to_html(self, buf, encoding, classes, notebook, border, table_id, render_links)
    992 else:
    993     created_buffer = False
    995 csv_formatter = CSVFormatter(
    996     path_or_buf=path_or_buf,
    997     lineterminator=lineterminator,
    998     sep=sep,
    999     encoding=encoding,
   1000     errors=errors,
   1001     compression=compression,
   1002     quoting=quoting,
   1003     cols=columns,
   1004     index_label=index_label,
   1005     mode=mode,
   1006     chunksize=chunksize,
-> 1007     quotechar=quotechar,
   1008     date_format=date_format,
   1009     doublequote=doublequote,
   1010     escapechar=escapechar,
   1011     storage_options=storage_options,
   1012     formatter=self.fmt,
   1013 )
   1014 csv_formatter.save()
   1016 if created_buffer:

File ~\.conda\envs\quantra_py\lib\site-packages\pandas\io\formats\html.py:88, in HTMLFormatter.to_string(self)
     87 def to_string(self) -> str:
---> 88     lines = self.render()
     89     if any(isinstance(x, str) for x in lines):
     90         lines = [str(x) for x in lines]

File ~\.conda\envs\quantra_py\lib\site-packages\pandas\io\formats\html.py:644, in NotebookFormatter.render(self)
    642 self.write("<div>")
    643 self.write_style()
--> 644 super().render()
    645 self.write("</div>")
    646 return self.elements

File ~\.conda\envs\quantra_py\lib\site-packages\pandas\io\formats\html.py:94, in HTMLFormatter.render(self)
     93 def render(self) -> list[str]:
---> 94     self._write_table()
     96     if self.should_show_dimensions:
     97         by = chr(215)  # ×  # noqa: RUF003

File ~\.conda\envs\quantra_py\lib\site-packages\pandas\io\formats\html.py:267, in HTMLFormatter._write_table(self, indent)
    261 self.write(
    262     f'<table{border_attr} class="{" ".join(_classes)}"{id_section}>',
    263     indent,
    264 )
    266 if self.fmt.header or self.show_row_idx_names:
--> 267     self._write_header(indent + self.indent_delta)
    269 self._write_body(indent + self.indent_delta)
    271 self.write("</table>", indent)

File ~\.conda\envs\quantra_py\lib\site-packages\pandas\io\formats\html.py:403, in HTMLFormatter._write_header(self, indent)
    400 self.write("<thead>", indent)
    402 if self.fmt.header:
--> 403     self._write_col_header(indent + self.indent_delta)
    405 if self.show_row_idx_names:
    406     self._write_row_header(indent + self.indent_delta)

File ~\.conda\envs\quantra_py\lib\site-packages\pandas\io\formats\html.py:383, in HTMLFormatter._write_col_header(self, indent)
    381     else:
    382         row.append("")
--> 383 row.extend(self._get_columns_formatted_values())
    384 align = self.fmt.justify
    386 if is_truncated_horizontally:

File ~\.conda\envs\quantra_py\lib\site-packages\pandas\io\formats\html.py:611, in NotebookFormatter._get_columns_formatted_values(self)
    609 def _get_columns_formatted_values(self) -> list[str]:
    610     # only reached with non-Multi Index
--> 611     return self.columns._format_flat(include_name=False)

AttributeError: 'Index' object has no attribute '_format_flat'


Out[15]:
Empty DataFrame
Columns: [Open, High, Low, Close, Adj Close, Volume]
Index: []

Hi Mayuresh,



This error might be due to some issues with the pandas library version on your system. I would suggest you to install pandas version 1.2.4 using the below command and then try running the code again.

!pip install pandas==1.2.4

To install the specified pandas version, you can type and run the above command in the Jupyter Notebook. Please let us know if you still face the issue.

 

THanks Akshay for your reply, but I am using Pandas 1.2.4 version only as per the quantra environment



Hi Mayuresh,



This was discussed and resolved.