JerBouma / FinanceToolkit

Transparent and Efficient Financial Analysis
https://www.jeroenbouma.com/projects/financetoolkit
MIT License
2.84k stars 347 forks source link

ValueError: Can't clean for JSON: Period('2018', 'Y-DEC') [BUG] #103

Closed kurucan closed 8 months ago

kurucan commented 8 months ago

import pandas as pd

from financetoolkit import Toolkit

Initialize the Toolkit with company tickers

companies = Toolkit( ["AAPL", "AMZN", "META"], api_key=API_KEY, start_date="2005-01-01" ) companies.ratios.collect_all_ratios()

gives the error below:


TypeError Traceback (most recent call last) File /shared-libs/python3.10/py-core/lib/python3.10/site-packages/jupyter_client/session.py:99, in json_packer(obj) 98 try: ---> 99 return json.dumps( 100 obj, 101 default=json_default, 102 ensure_ascii=False, 103 allow_nan=False, 104 ).encode("utf8", errors="surrogateescape") 105 except (TypeError, ValueError) as e: 106 # Fallback to trying to clean the json before serializing

File /usr/local/lib/python3.10/json/init.py:238, in dumps(obj, skipkeys, ensure_ascii, check_circular, allow_nan, cls, indent, separators, default, sort_keys, kw) 233 cls = JSONEncoder 234 return cls( 235 skipkeys=skipkeys, ensure_ascii=ensure_ascii, 236 check_circular=check_circular, allow_nan=allow_nan, indent=indent, 237 separators=separators, default=default, sort_keys=sort_keys, --> 238 kw).encode(obj)

File /usr/local/lib/python3.10/json/encoder.py:199, in JSONEncoder.encode(self, o) 196 # This doesn't pass the iterator directly to ''.join() because the 197 # exceptions aren't as detailed. The list call should be roughly 198 # equivalent to the PySequence_Fast that ''.join() would do. --> 199 chunks = self.iterencode(o, _one_shot=True) 200 if not isinstance(chunks, (list, tuple)):

File /usr/local/lib/python3.10/json/encoder.py:257, in JSONEncoder.iterencode(self, o, _one_shot) 253 _iterencode = _make_iterencode( 254 markers, self.default, _encoder, self.indent, floatstr, 255 self.key_separator, self.item_separator, self.sort_keys, 256 self.skipkeys, _one_shot) --> 257 return _iterencode(o, 0)

File /shared-libs/python3.10/py-core/lib/python3.10/site-packages/jupyter_client/jsonutil.py:126, in json_default(obj) 124 return float(obj) --> 126 raise TypeError("%r is not JSON serializable" % obj)

TypeError: Period('2018', 'Y-DEC') is not JSON serializable

During handling of the above exception, another exception occurred:

ValueError Traceback (most recent call last) Cell In [10], line 1 ----> 1 companies.ratios.collect_all_ratios()

File /shared-libs/python3.10/py-core/lib/python3.10/site-packages/IPython/core/displayhook.py:268, in DisplayHook.call(self, result) 266 self.write_format_data(format_dict, md_dict) 267 self.log_output(format_dict) --> 268 self.finish_displayhook()

File /shared-libs/python3.10/py-core/lib/python3.10/site-packages/ipykernel/displayhook.py:89, in ZMQShellDisplayHook.finish_displayhook(self) 87 sys.stderr.flush() 88 if self.msg["content"]["data"]: ---> 89 self.session.send(self.pub_socket, self.msg, ident=self.topic) 90 self.msg = None

File /shared-libs/python3.10/py-core/lib/python3.10/site-packages/jupyter_client/session.py:848, in Session.send(self, stream, msg_or_type, content, parent, ident, buffers, track, header, metadata) 846 if self.adapt_version: 847 msg = adapt(msg, self.adapt_version) --> 848 to_send = self.serialize(msg, ident) 849 to_send.extend(buffers) 850 longest = max([len(s) for s in to_send])

File /shared-libs/python3.10/py-core/lib/python3.10/site-packages/jupyter_client/session.py:718, in Session.serialize(self, msg, ident) 716 content = self.none 717 elif isinstance(content, dict): --> 718 content = self.pack(content) 719 elif isinstance(content, bytes): 720 # content is already packed, as in a relayed message 721 pass

File /shared-libs/python3.10/py-core/lib/python3.10/site-packages/jupyter_client/session.py:108, in json_packer(obj) 99 return json.dumps( 100 obj, 101 default=json_default, 102 ensure_ascii=False, 103 allow_nan=False, 104 ).encode("utf8", errors="surrogateescape") 105 except (TypeError, ValueError) as e: 106 # Fallback to trying to clean the json before serializing 107 packed = json.dumps( --> 108 json_clean(obj), 109 default=json_default, 110 ensure_ascii=False, 111 allow_nan=False, 112 ).encode("utf8", errors="surrogateescape") 114 warnings.warn( 115 f"Message serialization failed with:\n{e}\n" 116 "Supporting this message is deprecated in jupyter-client 7, please make " 117 "sure your message is JSON-compliant", 118 stacklevel=2, 119 ) 121 return packed

File /shared-libs/python3.10/py-core/lib/python3.10/site-packages/jupyter_client/jsonutil.py:185, in json_clean(obj) 183 out = {} 184 for k, v in obj.items(): --> 185 out[str(k)] = json_clean(v) 186 return out 188 if isinstance(obj, datetime):

File /shared-libs/python3.10/py-core/lib/python3.10/site-packages/jupyter_client/jsonutil.py:185, in json_clean(obj) 183 out = {} 184 for k, v in obj.items(): --> 185 out[str(k)] = json_clean(v) 186 return out 188 if isinstance(obj, datetime):

File /shared-libs/python3.10/py-core/lib/python3.10/site-packages/jupyter_client/jsonutil.py:185, in json_clean(obj) 183 out = {} 184 for k, v in obj.items(): --> 185 out[str(k)] = json_clean(v) 186 return out 188 if isinstance(obj, datetime):

File /shared-libs/python3.10/py-core/lib/python3.10/site-packages/jupyter_client/jsonutil.py:169, in json_clean(obj) 166 obj = list(obj) 168 if isinstance(obj, list): --> 169 return [json_clean(x) for x in obj] 171 if isinstance(obj, dict): 172 # First, validate that the dict won't lose data in conversion due to 173 # key collisions after stringification. This can happen with keys like 174 # True and 'true' or 1 and '1', which collide in JSON. 175 nkeys = len(obj)

File /shared-libs/python3.10/py-core/lib/python3.10/site-packages/jupyter_client/jsonutil.py:169, in (.0) 166 obj = list(obj) 168 if isinstance(obj, list): --> 169 return [json_clean(x) for x in obj] 171 if isinstance(obj, dict): 172 # First, validate that the dict won't lose data in conversion due to 173 # key collisions after stringification. This can happen with keys like 174 # True and 'true' or 1 and '1', which collide in JSON. 175 nkeys = len(obj)

File /shared-libs/python3.10/py-core/lib/python3.10/site-packages/jupyter_client/jsonutil.py:185, in json_clean(obj) 183 out = {} 184 for k, v in obj.items(): --> 185 out[str(k)] = json_clean(v) 186 return out 188 if isinstance(obj, datetime):

File /shared-libs/python3.10/py-core/lib/python3.10/site-packages/jupyter_client/jsonutil.py:192, in json_clean(obj) 189 return obj.strftime(ISO8601) 191 # we don't understand it, it's probably an unserializable object --> 192 raise ValueError("Can't clean for JSON: %r" % obj)

ValueError: Can't clean for JSON: Period('2018', 'Y-DEC')

JerBouma commented 8 months ago

This is a very odd error! I'll look into it but I can't reproduce it at the moment. Any ideas/pointers why you think this happens?

JerBouma commented 8 months ago

I can not reproduce this error. Please provide additional details about the version you are using of the Finance Toolkit and your Python version.

image
kurucan commented 7 months ago

I'm encountering this issue only when using Pandas v2.1 or lower on Python 3.11-3.10-3.9. While the package works seamlessly with Pandas v2.2, I get none of this errors. My current environment restrict me to use Pandas v2.2. Which version of this package should I install for the compability with Pandas 2.1. or lower ?

JerBouma commented 7 months ago

Downgrade to v1.8.0 which still uses Pandas v2.1