Rely on array backend for string formatting#6823
Conversation
|
Hmm, I'm not a fan that f-strings by default uses the backend array only: import pandas as pd
import xarray as xr
s = pd.Series(
range(4),
index=pd.MultiIndex.from_product([list("ab"), list("cd")]),
)
da = xr.DataArray.from_series(s, sparse=True)
# Uses __format__:
print(f"Error: {da} is sparse")
Error: <COO: shape=(2, 2), dtype=float64, nnz=4, fill_value=nan> is sparseShould it by default just use the repr instead? But it won't feel very consistent once string formatting are used though. |
for more information, see https://pre-commit.ci
|
This is how it behaves right now. I believe this should be the same behavior as previously now with the extra possibility to format scalar arrays: import pandas as pd
import xarray as xr
s = pd.Series(
range(4),
index=pd.MultiIndex.from_product([list("ab"), list("cd")]),
)
da = xr.DataArray.from_series(s, sparse=True)
# Handle sparse:
print(f"Error: {da} is sparse")
Error: <xarray.DataArray (level_0: 2, level_1: 2)>
<COO: shape=(2, 2), dtype=float64, nnz=4, fill_value=nan>
Coordinates:
* level_0 (level_0) object 'a' 'b'
* level_1 (level_1) object 'c' 'd' is sparse
# Handle format_spec:
da = xr.DataArray([1, 2, 3])
print(f'{da[0]}')
<xarray.DataArray ()>
array(1)
print(f'{da[0]:d}')
1
print(f'{da[0]:f}')
1.000000
da = xr.DataArray([1, 2, 3])
print(f'{da:.f}')
Traceback (most recent call last):
(...)
NotImplementedError: Using format_spec is only supported when shape is (). Got shape = (3,). |
I guess we could introduce some sort of
|
Interesting idea. I was thinking just adding Anyway that's for another PR. I think this PR is ready now, I think it should handle the main request in #5976 without being so intrusive in other cases. |
|
Great idea — doing anything that's expensive on |
.valuesto force to numpy arrays which doesn't work for sparse arrays and might be scary in the case of large dask arrays.format_specwhen dealing with scalar arrays, shape=(). Using f-strings is a common method to print the repr. If we want to have full support of.__format__we should probably addformat_specas input toformatting.array_repr.whats-new.rst