I am trying to import a json, an extract of a telegram channel, and then translate it. When I translate it, I get the following error. I initially thought the json may be too large, so I reduced it, but to no avail. ( I have made #comments to make the code as comprehensive as possible.)
import pandas as pd
import json
import re
#Opening and loading the json
f = open('result.json', encoding='utf8')
data = json.load(f)
#Retreive only the messages tag from json
msgs = data['messages']
dmain = pd.DataFrame(msgs)
pd.set_option('display.max_columns', None)
pd.set_option('display.max_rows', None)
#Put data into dataframe
df2 = dmain.filter(items = ['id','from', 'date' ,'text', 'type', 'reply_to_message_id'])
df2 = df2.head(10)
#Translates data (message and sender) from Farsi to English
import googletrans
from googletrans import *
translator = googletrans.Translator()
df2['from'] = df2['from'].astype(str)
df2['from'] = df2['from'].apply(translator.translate,src='auto',dest='en').apply(getattr,args=('text',))
df2['text'] = df2['text'].astype(str)
df2['text'] = df2['text'].apply(translator.translate,src='auto',dest='en').apply(getattr,args=('text',))
# pd.set_option('max_colwidth', 1000)
df2.head(10)
Below is the error I get when I run the code.
TypeError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_31688/502556507.py in <module>
8
9 df2['text'] = df2['text'].astype(str)
---> 10 df2['text'] = df2['text'].apply(translator.translate,src='auto',dest='en').apply(getattr,args=('text',))
11
12
~\anaconda3\lib\site-packages\pandas\core\series.py in apply(self, func, convert_dtype, args, **kwargs)
4355 dtype: float64
4356 """
-> 4357 return SeriesApply(self, func, convert_dtype, args, kwargs).apply()
4358
4359 def _reduce(
~\anaconda3\lib\site-packages\pandas\core\apply.py in apply(self)
1041 return self.apply_str()
1042
-> 1043 return self.apply_standard()
1044
1045 def agg(self):
~\anaconda3\lib\site-packages\pandas\core\apply.py in apply_standard(self)
1096 # List[Union[Callable[..., Any], str]]]]]"; expected
1097 # "Callable[[Any], Any]"
-> 1098 mapped = lib.map_infer(
1099 values,
1100 f, # type: ignore[arg-type]
~\anaconda3\lib\site-packages\pandas\_libs\lib.pyx in pandas._libs.lib.map_infer()
~\anaconda3\lib\site-packages\pandas\core\apply.py in f(x)
129
130 def f(x):
--> 131 return func(x, *args, **kwargs)
132
133 else:
~\anaconda3\lib\site-packages\googletrans\client.py in translate(self, text, dest, src)
217
218 data = json.loads(resp)
--> 219 parsed = json.loads(data[0][2])
220 # not sure
221 should_spacing = parsed[1][0][0][3]
~\anaconda3\lib\json\__init__.py in loads(s, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
337 else:
338 if not isinstance(s, (bytes, bytearray)):
--> 339 raise TypeError(f'the JSON object must be str, bytes or bytearray, '
340 f'not {s.__class__.__name__}')
341 s = s.decode(detect_encoding(s), 'surrogatepass')
TypeError: the JSON object must be str, bytes or bytearray, not NoneType