8000 fix #880 : implemented Axis.astype() method · larray-project/larray@f482382 · GitHub
[go: up one dir, main page]

Skip to content

Commit f482382

Browse files
committed
fix #880 : implemented Axis.astype() method
1 parent ce562be commit f482382

File tree

4 files changed

+56
-1
lines changed

4 files changed

+56
-1
lines changed

doc/source/api.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ Modifying/Selecting
7171
Axis.align
7272
Axis.split
7373
Axis.ignore_labels
74+
Axis.astype
7475

7576
Testing
7677
-------

doc/source/changes/version_0_33.rst.inc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ Miscellaneous improvements
5454
* scalar objects (i.e of type int, float, bool, string, date, time or datetime) belonging to a session
5555
are now also saved and loaded when using the HDF5 or pickle format (closes :issue:`842`).
5656

57+
* implemented :py:obj:`Axis.astype()` method (closes :issue:`880`).
5758

5859
Fixes
5960
^^^^^

larray/core/axis.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import warnings
55
from itertools import product
66

7+
from typing import Union
8+
79
import numpy as np
810
import pandas as pd
911

@@ -234,6 +236,49 @@ def by(self, length, step=None, template=None):
234236
"""
235237
return self[:].by(length, step, template)
236238

239+
def astype(self, dtype: Union[str, np.dtype], casting: str = 'unsafe') -> 'Axis':
240+
"""
241+
Cast labels to a specified type.
242+
243+
Parameters
244+
----------
245+
dtype: str or dtype
246+
Typecode or data-type to which the labels are cast.
247+
248+
casting: str, optional
249+
Controls what kind of data casting may occur. Defaults to `unsafe`.
250+
251+
* `no` means the data types should not be cast at all.
252+
* `equiv` means only byte-order changes are allowed.
253+
* `safe` means only casts which can preserve values are allowed.
254+
* `same_kind` means only safe casts or casts within a kind, like float64 to float32, are allowed.
255+
* `unsafe` means any data conversions may be done.
256+
257+
Returns
258+
-------
259+
Axis
260+
Axis with labels converted to the new type.
261+
262+
Examples
263+
--------
264+
>>> from larray import ndtest
265+
>>> arr = ndtest('time=2015..2020')
266+
>>> arr = arr.with_total()
267+
>>> arr
268+
time 2015 2016 2017 2018 2019 2020 total
269+
0 1 2 3 4 5 15
270+
>>> arr = arr.drop('total')
271+
>>> time = arr.time
272+
>>> time
273+
Axis([2015, 2016, 2017, 2018, 2019, 2020], 'time')
274+
>>> time.dtype
275+
dtype('O')
276+
>>> time = time.astype(int)
277+
>>> time.dtype # doctest: +SKIP
278+
dtype('int64')
279+
"""
280+
return Axis(labels=self.labels.astype(dtype=dtype, casting=casting), name=self.name)
281+
237282
def extend(self, labels):
238283
r"""
239284
Append new labels to an axis or increase its length in case of wildcard axis.

larray/tests/test_axis.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import numpy as np
44

55
from larray.tests.common import assert_array_equal, assert_nparray_equal, needs_pytables
6-
from larray import Axis, LGroup, IGroup, read_hdf, X
6+
from larray import Axis, LGroup, IGroup, read_hdf, X, ndtest
77
from larray.core.axis import AxisReference
88

99

@@ -94,6 +94,14 @@ def test_translate():
9494
assert a.index('a1 >> A1') == 1
9595

9696

97+
def test_astype():
98+
arr = ndtest(Axis('time=2015..2020,total')).drop('total')
99+
time = arr.time
100+
assert time.dtype.kind == 'U'
101+
time = time.astype(int)
102+
assert time.dtype.kind == 'i'
103+
104+
97105
def test_getitem_lgroup_keys():
98106
def group_equal(g1, g2):
99107
return g1.key == g2.key and g1.name == g2.name and g1.axis is g2.axis

0 commit comments

Comments
 (0)
0