Skip to content

Commit fbbd31b

Browse files
committed
small code cleanups in various functions
incidentally it also makes .reshape support string axes collection... not that it matters much since it should be private method these days anyway
1 parent 6b18504 commit fbbd31b

File tree

5 files changed

+18
-19
lines changed

5 files changed

+18
-19
lines changed

larray/core/array.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1333,10 +1333,10 @@ def rename(self, renames=None, to=None, inplace=False, **kwargs):
13331333
items = []
13341334
items += kwargs.items()
13351335
renames = {self.axes[k]: v for k, v in items}
1336-
axes = [a.rename(renames[a]) if a in renames else a
1337-
for a in self.axes]
1336+
axes = AxisCollection([a.rename(renames[a]) if a in renames else a
1337+
for a in self.axes])
13381338
if inplace:
1339-
self.axes = AxisCollection(axes)
1339+
self.axes = axes
13401340
return self
13411341
else:
13421342
return LArray(self.data, axes)
@@ -1870,8 +1870,7 @@ def sort_key(axis):
18701870
key = key[::-1]
18711871
return axis.i[key]
18721872

1873-
res = self[tuple(sort_key(axis) for axis in axes)]
1874-
return res
1873+
return self[tuple(sort_key(axis) for axis in axes)]
18751874

18761875
sort_axis = renamed_to(sort_axes, 'sort_axis')
18771876

@@ -2113,6 +2112,7 @@ def set(self, value, **kwargs):
21132112
"""
21142113
self.__setitem__(kwargs, value)
21152114

2115+
# TODO: this should be a private method
21162116
def reshape(self, target_axes):
21172117
"""
21182118
Given a list of new axes, changes the shape of the array.
@@ -2157,9 +2157,12 @@ def reshape(self, target_axes):
21572157
# -> 3, 8 WRONG (non adjacent dimensions)
21582158
# -> 8, 3 WRONG
21592159
# 4, 3, 2 -> 2, 2, 3, 2 is potentially ok (splitting dim)
2160-
data = np.asarray(self).reshape([len(axis) for axis in target_axes])
2160+
if not isinstance(target_axes, AxisCollection):
2161+
target_axes = AxisCollection(target_axes)
2162+
data = np.asarray(self).reshape(target_axes.shape)
21612163
return LArray(data, target_axes)
21622164

2165+
# TODO: this should be a private method
21632166
def reshape_like(self, target):
21642167
"""
21652168
Same as reshape but with an array as input.
@@ -6372,15 +6375,12 @@ def to_clipboard(self, *args, **kwargs):
63726375
self.to_frame().to_clipboard(*args, **kwargs)
63736376

63746377
# XXX: sep argument does not seem very useful
6375-
# def to_excel(self, filename, sep=None):
6378+
# def to_excel(self, filename, sep='_'):
63766379
# # Why xlsxwriter? Because it is faster than openpyxl and xlwt
63776380
# # currently does not .xlsx (only .xls).
63786381
# # PyExcelerate seem like a decent alternative too
63796382
# import xlsxwriter as xl
63806383
#
6381-
# if sep is None:
6382-
# sep = '_'
6383-
# #sep = self.sep
63846384
# workbook = xl.Workbook(filename)
63856385
# if self.ndim > 2:
63866386
# for key in product(*[axis.labels for axis in self.axes[:-2]]):
@@ -8529,7 +8529,7 @@ def raw_broadcastable(values, min_axes=None):
85298529
"""
85308530
same as make_numpy_broadcastable but returns numpy arrays
85318531
"""
8532-
arrays, res_axes = make_numpy_broadcastable(values, min_axes)
8532+
arrays, res_axes = make_numpy_broadcastable(values, min_axes=min_axes)
85338533
raw = [a.data if isinstance(a, LArray) else a
85348534
for a in arrays]
85358535
return raw, res_axes

larray/core/group.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ def _slice_to_str(key, repr_func=str):
3333
>>> _slice_to_str(slice(None, 5, 2))
3434
':5:2'
3535
"""
36-
# examples of result: ":24" "25:" ":" ":5:2"
3736
start = repr_func(key.start) if key.start is not None else ''
3837
stop = repr_func(key.stop) if key.stop is not None else ''
3938
step = (":" + repr_func(key.step)) if key.step is not None else ''

larray/inout/pandas.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import pandas as pd
88

99
from larray.core.array import LArray
10-
from larray.core.axis import Axis
10+
from larray.core.axis import Axis, AxisCollection
1111
from larray.core.group import LGroup
1212
from larray.core.constants import nan
1313
from larray.util.misc import basestring, decode, unique
@@ -67,7 +67,7 @@ def cartesian_product_df(df, sort_rows=False, sort_columns=False, fill_value=nan
6767
columns = sorted(df.columns) if sort_columns else list(df.columns)
6868
# the prodlen test is meant to avoid the more expensive array_equal test
6969
prodlen = np.prod([len(axis_labels) for axis_labels in labels])
70-
if prodlen == len(df) and columns == list(df.columns) and np.array_equal(df.index.values, new_index.values):
70+
if prodlen == len(df) and columns == list(df.columns) and np.array_equal(idx.values, new_index.values):
7171
return df, labels
7272
return df.reindex(index=new_index, columns=columns, fill_value=fill_value, **kwargs), labels
7373

@@ -247,8 +247,8 @@ def from_frame(df, sort_rows=False, sort_columns=False, parse_header=False, unfo
247247
axes_names = [str(name) if name is not None else name
248248
for name in axes_names]
249249

250-
axes = [Axis(labels, name) for labels, name in zip(axes_labels, axes_names)]
251-
data = df.values.reshape([len(axis) for axis in axes])
250+
axes = AxisCollection([Axis(labels, name) for labels, name in zip(axes_labels, axes_names)])
251+
data = df.values.reshape(axes.shape)
252252
return LArray(data, axes, meta=meta)
253253

254254

larray/random.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
import numpy as np
2727

2828
from larray.core.axis import Axis, AxisCollection
29-
from larray.core.array import LArray, aslarray, stack, ndtest
29+
from larray.core.array import LArray, aslarray
3030
from larray.core.array import raw_broadcastable
3131
import larray as la
3232

@@ -35,7 +35,7 @@
3535

3636

3737
def generic_random(np_func, args, min_axes, meta):
38-
args, res_axes = raw_broadcastable(args, min_axes)
38+
args, res_axes = raw_broadcastable(args, min_axes=min_axes)
3939
res_data = np_func(*args, size=res_axes.shape)
4040
return LArray(res_data, res_axes, meta=meta)
4141

larray/util/misc.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ def is_interactive_interpreter():
6363

6464
def csv_open(filename, mode='r'):
6565
assert 'b' not in mode and 't' not in mode
66-
if sys.version < '3':
66+
if PY2:
6767
return open(filename, mode + 'b')
6868
else:
6969
return open(filename, mode, newline='', encoding='utf8')

0 commit comments

Comments
 (0)