-
-
Notifications
You must be signed in to change notification settings - Fork 18.2k
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
REF: remove Block access in the JSON writing code #41081
REF: remove Block access in the JSON writing code #41081
Conversation
It might be that with a bit of optimization of |
|
||
blkCtxt->cindices[colIdx] = idx; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think if you remove this assignment to cindices
you can remove this struct member altogether - not aware of it being written to anywhere else. Might help reduce confusion over state management here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, indeed, there are still some further things to clean up. And this cindices
is now no longer used.
+1 on deleting the block code |
3c9aa99
to
e0f32f2
Compare
With the last commit (adding some extra python code to more efficiently get a list of 1D ndarrays for BlockManager), I now get hose ASV results (for the same before after ratio
[692bba57] [3c9aa994]
<master> <refactor-json-blocks>
+ 114±0.6ms 122±2ms 1.07 io.json.ToJSON.time_to_json_wide('split', 'df_date_idx')
+ 152±1ms 162±2ms 1.06 io.json.ToJSON.time_to_json_wide('records', 'df')
+ 148±2ms 156±2ms 1.05 io.json.ToJSON.time_to_json_wide('columns', 'df')
- 79.8±0.2ms 78.8±0.2ms 0.99 io.json.NormalizeJSON.time_normalize_json('values', 'df_int_floats') So that looks quite good I think |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm when green - nice simplification!
@jreback do you want to review this more in depth? |
lgtm. need a rebase |
@@ -224,14 +224,6 @@ def get_values(self, dtype: DtypeObj | None = None) -> np.ndarray: | |||
# expected "ndarray") | |||
return self.values # type: ignore[return-value] | |||
|
|||
@final | |||
def get_block_values_for_json(self) -> np.ndarray: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i think the "final" is out of date; same method on DatetimeLikeBlock
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is only a single get_block_values_for_json
(no Block subclass overrides it)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or at least until a few hours ago, when your PR that changed this was merged ;) (You could have just as well said that you just changed this / pointed to #41082)
pandas/core/internals/managers.py
Outdated
This optimizes compared to using `iget_values` by converting each | ||
block.values to a np.ndarray only once up front | ||
""" | ||
arrays = [np.asarray(blk.values) for blk in self.blocks] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i think this will change the behavior for dt64tz
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will not change behaviour, as it is the same as get_block_values_for_json
did before until a few hours ago. The comment you added to the changed implementation in #41082 is "Not necessary to override, but helps perf", so assuming that that didn't change behaviour.
I would also assume it is covered by the tests, but checking explicitly:
Master:
In [1]: pd.DataFrame({'a': [1, 2], 'b': pd.date_range("2012", periods=2, tz="Europe/Brussels")}).to_json()
Out[1]: '{"a":{"0":1,"1":2},"b":{"0":1325372400000,"1":1325458800000}}'
In [2]: pd.DataFrame({'b': pd.date_range("2012", periods=2, tz="Europe/Brussels")}).to_json()
Out[2]: '{"b":{"0":1325372400000,"1":1325458800000}}'
PR:
In [1]: pd.DataFrame({'a': [1, 2], 'b': pd.date_range("2012", periods=2, tz="Europe/Brussels")}).to_json()
Out[1]: '{"a":{"0":1,"1":2},"b":{"0":1325372400000,"1":1325458800000}}'
In [2]: pd.DataFrame({'b': pd.date_range("2012", periods=2, tz="Europe/Brussels")}).to_json()
Out[2]: '{"b":{"0":1325372400000,"1":1325458800000}}'
Now, to preserve the performance improvement of #41082, I can add a check for datetimetz.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hopefully more accurate statement: i think this will entail an object-dtype conversion for dt64tz. I'm not especially bothered by this bc i think this is a good cleanup
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
... which i now see you already addressed in a new commit. carry on
Failure is a flaky resourcewarning. |
Closes #27164
This is a small refactor of the JSON writing code (
objToJSON.c
) to remove the access of the Blocks. Serializing to JSON was already done column-by-column (also when accessing the blocks), but currently we were iterating over the different "columns" of the 2D array per block in the C code. While with this PR, the C code directly iterates over all the columns (instead of per block).This gives a less invasive change as the previous attempt of #27166
To be able to iterate over the column arrays in C, I added the
column_arrays
attribute on the manager, for this.I ran the JSON ASV benchmarks with this branch:
$ asv continuous -b json -f 1.01 upstream/master HEAD
There is a slight slowdown in some of the benchmarks for wide dataframes, but only 20%.
So we can decide to keep the original block-iteration code for BlockManager for now, instead of fully replacing it as I did now, if we don't want this slight slowdown.
This also fixes some remaining cases for ArrayManager (xref #39146)