Skip to content

[BUG] handle } in line delimited json #14391

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 11 commits into from
Closed
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions asv_bench/benchmarks/packers.py
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,36 @@ def remove(self, f):
pass


class packers_write_json_lines(object):
goal_time = 0.2

def setup(self):
self.f = '__test__.msg'
self.N = 100000
self.C = 5
self.index = date_range('20000101', periods=self.N, freq='H')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks like some things got repeated here

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like self.N/self.C are repeated in most of these classes. Want me to clean them all up?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure that would be great (you can also make a common base class(s) if that helps as well)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@joshowen you can leave it here as is. I cleaned this up in another PR (@jreback yes I know, I should merge that ...)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok that's fine (though @joshowen make sure your example doesn't have dups as this is new code)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, yes, it is of course OK to remove the lines in this added code that you do not need for this benchmark

self.df = DataFrame(dict([('float{0}'.format(i), randn(self.N)) for i in range(self.C)]), index=self.index)
self.N = 100000
self.C = 5
self.index = date_range('20000101', periods=self.N, freq='H')
self.df2 = DataFrame(dict([('float{0}'.format(i), randn(self.N)) for i in range(self.C)]), index=self.index)
self.df2['object'] = [('%08x' % randrange((16 ** 8))) for _ in range(self.N)]
self.remove(self.f)
self.df.index = np.arange(self.N)

def time_packers_write_json_lines(self):
self.df.to_json(self.f, orient="records", lines=True)

def teardown(self):
self.remove(self.f)

def remove(self, f):
try:
os.remove(self.f)
except:
pass


class packers_write_json_T(object):
goal_time = 0.2

Expand Down
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.19.1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,4 @@ Bug Fixes

- Bug in ``pd.concat`` where names of the ``keys`` were not propagated to the resulting ``MultiIndex`` (:issue:`14252`)
- Bug in ``MultiIndex.set_levels`` where illegal level values were still set after raising an error (:issue:`13754`)
- Bug in ``DataFrame.to_json`` where ``lines=True`` and a value contained a ``}`` character (:issue:`14391`)
13 changes: 9 additions & 4 deletions pandas/io/json.py
Original file line number Diff line number Diff line change
Expand Up @@ -607,14 +607,19 @@ def _convert_to_line_delimits(s):
s = s[1:-1]
num_open_brackets_seen = 0
commas_to_replace = []
in_quotes = False
for idx, char in enumerate(s): # iter through to find all
if char == ',': # commas that should be \n
if num_open_brackets_seen == 0:
if char == '"' and idx > 0 and s[idx - 1] != '\\':
in_quotes = ~in_quotes
elif char == ',': # commas that should be \n
if num_open_brackets_seen == 0 and not in_quotes:
commas_to_replace.append(idx)
elif char == '{':
num_open_brackets_seen += 1
if not in_quotes:
num_open_brackets_seen += 1
elif char == '}':
num_open_brackets_seen -= 1
if not in_quotes:
num_open_brackets_seen -= 1
s_arr = np.array(list(s)) # Turn to an array to set
s_arr[commas_to_replace] = '\n' # all commas at once.
s = ''.join(s_arr)
Expand Down
5 changes: 5 additions & 0 deletions pandas/io/tests/json/test_pandas.py
Original file line number Diff line number Diff line change
Expand Up @@ -962,6 +962,11 @@ def test_to_jsonl(self):
expected = '{"a":1,"b":2}\n{"a":1,"b":2}'
self.assertEqual(result, expected)

df = DataFrame([["foo}", "bar"], ['foo"', "bar"]], columns=['a', 'b'])
result = df.to_json(orient="records", lines=True)
expected = '{"a":"foo}","b":"bar"}\n{"a":"foo\\"","b":"bar"}'
self.assertEqual(result, expected)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you also round trip it and user assert_frame_equal on the result (in addition to the above test)


def test_latin_encoding(self):
if compat.PY2:
self.assertRaisesRegexp(
Expand Down