ARahimiQuant / finpy-tse

A Python Package to Access Tehran Stock Exchange Historical and Real-Time Data
https://pypi.org/project/finpy-tse/
BSD 3-Clause "New" or "Revised" License
72 stars 12 forks source link

error when calling Get_Price_History #13

Closed abootorabi closed 6 months ago

abootorabi commented 6 months ago

im trying to get price history of all Tickers but i get "AttributeError: Can only use .dt accessor with datetimelike values" and it does not happen all the time most of the time at at first Ticker gets this Error . and sometimes it passes and 2th or 3th ticker get this error .


AttributeError Traceback (most recent call last) Cell In[11], line 4 1 for i in stocklist.index: 3 print(i) ----> 4 s = fpy.Get_Price_History(stock=i , 5 start_date='1390-01-01', 6 end_date='1403-03-01', 7 ignore_date=False, 8 adjust_price=True, 9 show_weekday=False, 10 double_date=False) 12 time.sleep(1) 13 s.to_csv('./Tickers_data/'+ i + '.csv')

File ~\AppData\Local\Programs\Python\Python312\Lib\site-packages\finpy_tse__init__.py:431, in Get_Price_History(stock, start_date, end_date, ignore_date, adjust_price, show_weekday, double_date) 429 df_history = df_history.reset_index() 430 # determining week days: --> 431 df_history['Weekday']=df_history['Date'].dt.weekday 432 df_history['Weekday'] = df_history['Weekday'].apply(lambda x: calendar.day_name[x]) 433 df_history['J-Date']=df_history['Date'].apply(lambda x: str(jdatetime.date.fromgregorian(date=x.date())))

File ~\AppData\Local\Programs\Python\Python312\Lib\site-packages\pandas\core\generic.py:6296, in NDFrame.getattr(self, name) 6289 if ( 6290 name not in self._internal_names_set 6291 and name not in self._metadata 6292 and name not in self._accessors 6293 and self._info_axis._can_hold_identifiers_and_holds_name(name) 6294 ): 6295 return self[name] -> 6296 return object.getattribute(self, name)

File ~\AppData\Local\Programs\Python\Python312\Lib\site-packages\pandas\core\accessor.py:224, in CachedAccessor.get(self, obj, cls) 221 if obj is None: 222 # we're accessing the attribute of the class, i.e., Dataset.geo 223 return self._accessor --> 224 accessor_obj = self._accessor(obj) 225 # Replace the property with the accessor object. Inspired by: 226 # https://www.pydanny.com/cached-property.html 227 # We need to use object.setattr because we overwrite setattr on 228 # NDFrame 229 object.setattr(obj, self._name, accessor_obj)

File ~\AppData\Local\Programs\Python\Python312\Lib\site-packages\pandas\core\indexes\accessors.py:643, in CombinedDatetimelikeProperties.new(cls, data) 640 elif isinstance(data.dtype, PeriodDtype): 641 return PeriodProperties(data, orig) --> 643 raise AttributeError("Can only use .dt accessor with datetimelike values")

AttributeError: Can only use .dt accessor with datetimelike values

ARahimiQuant commented 6 months ago

Sometimes, the TSE server does not respond properly and returns an empty response with a code 200 to our requests. When the response is not in the expected format, the data cleaning process can lead to errors, which are reported by other users and experienced by us while working with finpy_tse.

Considering these issues, and acknowledging that we are not dealing with an ideal, perfect, and always reliable server (i.e., TSE), there is little we can do to prevent such errors, as they do not relate to the finpy_tse code. Therefore, it is up to you as a coder to handle these types of errors by retrying the request. In upcoming versions of finpy_tse, we will raise a custom error for these types of issues, so users can understand the nature of the problem.