Skip to content

CLN: remove compat.lmap #26322

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

Merged
merged 1 commit into from
May 10, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 1 addition & 5 deletions pandas/compat/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
Cross-compatible functions for different versions of Python.

Key items to import for compatible code:
* lists: lrange(), lmap()
* lists: lrange()

Other items:
* platform checker
Expand All @@ -24,10 +24,6 @@ def lrange(*args, **kwargs):
return list(range(*args, **kwargs))


def lmap(*args, **kwargs):
return list(map(*args, **kwargs))


# ----------------------------------------------------------------------------
# functions largely based / taken from the six module

Expand Down
4 changes: 1 addition & 3 deletions pandas/core/computation/expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@

import numpy as np

from pandas.compat import lmap

import pandas as pd
from pandas.core import common as com
from pandas.core.base import StringMixin
Expand Down Expand Up @@ -180,7 +178,7 @@ def _preparse(source, f=_compose(_replace_locals, _replace_booleans,
the ``tokenize`` module and ``tokval`` is a string.
"""
assert callable(f), 'f must be callable'
return tokenize.untokenize(lmap(f, tokenize_string(source)))
return tokenize.untokenize((f(x) for x in tokenize_string(source)))


def _is_type(t):
Expand Down
9 changes: 5 additions & 4 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
from pandas.util._validators import (validate_bool_kwarg,
validate_axis_style_args)

from pandas.compat import PY36, lmap, raise_with_traceback
from pandas.compat import PY36, raise_with_traceback
from pandas.compat.numpy import function as nv
from pandas.core.dtypes.cast import (
maybe_upcast,
Expand Down Expand Up @@ -1633,7 +1633,7 @@ def to_records(self, index=True, convert_datetime64=None,
else:
if isinstance(self.index, MultiIndex):
# array of tuples to numpy cols. copy copy copy
ix_vals = lmap(np.array, zip(*self.index.values))
ix_vals = list(map(np.array, zip(*self.index.values)))
else:
ix_vals = [self.index.values]

Expand All @@ -1650,10 +1650,11 @@ def to_records(self, index=True, convert_datetime64=None,
elif index_names[0] is None:
index_names = ['index']

names = lmap(str, index_names) + lmap(str, self.columns)
names = [str(name) for name in itertools.chain(index_names,
self.columns)]
else:
arrays = [self[c].get_values() for c in self.columns]
names = lmap(str, self.columns)
names = [str(c) for c in self.columns]
index_names = []

index_len = len(index_names)
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/internals/construction.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

from pandas._libs import lib
from pandas._libs.tslibs import IncompatibleFrequency
from pandas.compat import lmap, raise_with_traceback
from pandas.compat import raise_with_traceback

from pandas.core.dtypes.cast import (
construct_1d_arraylike_from_scalar, construct_1d_ndarray_preserving_na,
Expand Down Expand Up @@ -413,7 +413,7 @@ def to_arrays(data, columns, coerce_float=False, dtype=None):
return arrays, columns
else:
# last ditch effort
data = lmap(tuple, data)
data = [tuple(x) for x in data]
return _list_to_arrays(data, columns, coerce_float=coerce_float,
dtype=dtype)

Expand Down
3 changes: 1 addition & 2 deletions pandas/core/sparse/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import numpy as np

from pandas._libs.sparse import BlockIndex, get_blocks
from pandas.compat import lmap
from pandas.compat.numpy import function as nv
from pandas.util._decorators import Appender

Expand Down Expand Up @@ -945,7 +944,7 @@ def applymap(self, func):
-------
applied : DataFrame
"""
return self.apply(lambda x: lmap(func, x))
return self.apply(lambda x: [func(y) for y in x])


def to_manager(sdf, columns, index):
Expand Down
4 changes: 1 addition & 3 deletions pandas/core/sparse/scipy_sparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
"""
from collections import OrderedDict

from pandas.compat import lmap

from pandas.core.index import Index, MultiIndex
from pandas.core.series import Series

Expand Down Expand Up @@ -53,7 +51,7 @@ def _get_label_to_i_dict(labels, sort_labels=False):
""" Return OrderedDict of unique labels to number.
Optionally sort by label.
"""
labels = Index(lmap(tuple, labels)).unique().tolist() # squish
labels = Index(map(tuple, labels)).unique().tolist() # squish
if sort_labels:
labels = sorted(list(labels))
d = OrderedDict((k, i) for i, k in enumerate(labels))
Expand Down
4 changes: 2 additions & 2 deletions pandas/io/html.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import os
import re

from pandas.compat import lmap, raise_with_traceback
from pandas.compat import raise_with_traceback
from pandas.errors import AbstractMethodError, EmptyDataError

from pandas.core.dtypes.common import is_list_like
Expand Down Expand Up @@ -764,7 +764,7 @@ def _parse_tfoot_tr(self, table):


def _expand_elements(body):
lens = Series(lmap(len, body))
lens = Series([len(elem) for elem in body])
lens_max = lens.max()
not_max = lens[lens != lens_max]

Expand Down
9 changes: 4 additions & 5 deletions pandas/io/stata.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@

from pandas._libs.lib import infer_dtype
from pandas._libs.writers import max_len_string_array
from pandas.compat import lmap
from pandas.util._decorators import Appender, deprecate_kwarg

from pandas.core.dtypes.common import (
Expand Down Expand Up @@ -1030,7 +1029,7 @@ def _read_header(self):
if type(x) is int]) > 0

# calculate size of a data record
self.col_sizes = lmap(lambda x: self._calcsize(x), self.typlist)
self.col_sizes = [self._calcsize(typ) for typ in self.typlist]

def _read_new_header(self, first_char):
# The first part of the header is common to 117 and 118.
Expand Down Expand Up @@ -1573,9 +1572,9 @@ def read(self, nrows=None, convert_dates=None,
data = self._do_convert_missing(data, convert_missing)

if convert_dates:
cols = np.where(lmap(lambda x: any(x.startswith(fmt)
for fmt in _date_formats),
self.fmtlist))[0]
def any_startswith(x: str) -> bool:
return any(x.startswith(fmt) for fmt in _date_formats)
cols = np.where([any_startswith(x) for x in self.fmtlist])[0]
for i in cols:
col = data.columns[i]
try:
Expand Down
4 changes: 2 additions & 2 deletions pandas/plotting/_misc.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# being a bit too dynamic
import numpy as np

from pandas.compat import lmap, lrange
from pandas.compat import lrange
from pandas.util._decorators import deprecate_kwarg

from pandas.core.dtypes.missing import notna
Expand Down Expand Up @@ -625,7 +625,7 @@ def r(h):
return ((data[:n - h] - mean) *
(data[h:] - mean)).sum() / float(n) / c0
x = np.arange(n) + 1
y = lmap(r, x)
y = [r(loc) for loc in x]
z95 = 1.959963984540054
z99 = 2.5758293035489004
ax.axhline(y=z99 / np.sqrt(n), linestyle='--', color='grey')
Expand Down
6 changes: 3 additions & 3 deletions pandas/plotting/_style.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import numpy as np

from pandas.compat import lmap, lrange
from pandas.compat import lrange

from pandas.core.dtypes.common import is_list_like

Expand All @@ -20,7 +20,7 @@ def _get_standard_colors(num_colors=None, colormap=None, color_type='default',
colormap = cm.get_cmap(colormap)
if colormap is None:
raise ValueError("Colormap {0} is not recognized".format(cmap))
colors = lmap(colormap, np.linspace(0, 1, num=num_colors))
colors = [colormap(num) for num in np.linspace(0, 1, num=num_colors)]
elif color is not None:
if colormap is not None:
warnings.warn("'color' and 'colormap' cannot be used "
Expand Down Expand Up @@ -49,7 +49,7 @@ def random_color(column):
rs = com.random_state(column)
return rs.rand(3).tolist()

colors = lmap(random_color, lrange(num_colors))
colors = [random_color(num) for num in lrange(num_colors)]
else:
raise ValueError("color_type must be either 'default' or 'random'")

Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/frame/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import numpy.ma as ma
import pytest

from pandas.compat import PY36, is_platform_little_endian, lmap
from pandas.compat import PY36, is_platform_little_endian

from pandas.core.dtypes.cast import construct_1d_object_array_from_listlike
from pandas.core.dtypes.common import is_integer_dtype
Expand Down Expand Up @@ -2224,7 +2224,7 @@ def __iter__(self):
return iter(self.args)

recs = [Record(1, 2, 3), Record(4, 5, 6), Record(7, 8, 9)]
tups = lmap(tuple, recs)
tups = [tuple(rec) for rec in recs]

result = DataFrame.from_records(recs)
expected = DataFrame.from_records(tups)
Expand Down
46 changes: 28 additions & 18 deletions pandas/tests/frame/test_to_csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import numpy as np
import pytest

from pandas.compat import lmap
from pandas.errors import ParserError

import pandas as pd
Expand Down Expand Up @@ -120,8 +119,8 @@ def test_to_csv_from_csv3(self):
df2.to_csv(path, mode='a', header=False)
xp = pd.concat([df1, df2])
rs = pd.read_csv(path, index_col=0)
rs.columns = lmap(int, rs.columns)
xp.columns = lmap(int, xp.columns)
rs.columns = [int(label) for label in rs.columns]
xp.columns = [int(label) for label in xp.columns]
assert_frame_equal(xp, rs)

def test_to_csv_from_csv4(self):
Expand Down Expand Up @@ -292,19 +291,24 @@ def _to_uni(x):
if r_dtype:
if r_dtype == 'u': # unicode
r_dtype = 'O'
recons.index = np.array(lmap(_to_uni, recons.index),
dtype=r_dtype)
df.index = np.array(lmap(_to_uni, df.index), dtype=r_dtype)
recons.index = np.array(
[_to_uni(label) for label in recons.index],
dtype=r_dtype)
df.index = np.array(
[_to_uni(label) for label in df.index], dtype=r_dtype)
elif r_dtype == 'dt': # unicode
r_dtype = 'O'
recons.index = np.array(lmap(Timestamp, recons.index),
dtype=r_dtype)
recons.index = np.array(
[Timestamp(label) for label in recons.index],
dtype=r_dtype)
df.index = np.array(
lmap(Timestamp, df.index), dtype=r_dtype)
[Timestamp(label) for label in df.index],
dtype=r_dtype)
elif r_dtype == 'p':
r_dtype = 'O'
idx_list = to_datetime(recons.index)
recons.index = np.array(
list(map(Timestamp, to_datetime(recons.index))),
[Timestamp(label) for label in idx_list],
dtype=r_dtype)
df.index = np.array(
list(map(Timestamp, df.index.to_timestamp())),
Expand All @@ -316,23 +320,29 @@ def _to_uni(x):
if c_dtype:
if c_dtype == 'u':
c_dtype = 'O'
recons.columns = np.array(lmap(_to_uni, recons.columns),
dtype=c_dtype)
recons.columns = np.array(
[_to_uni(label) for label in recons.columns],
dtype=c_dtype)
df.columns = np.array(
lmap(_to_uni, df.columns), dtype=c_dtype)
[_to_uni(label) for label in df.columns],
dtype=c_dtype)
elif c_dtype == 'dt':
c_dtype = 'O'
recons.columns = np.array(lmap(Timestamp, recons.columns),
dtype=c_dtype)
recons.columns = np.array(
[Timestamp(label) for label in recons.columns],
dtype=c_dtype)
df.columns = np.array(
lmap(Timestamp, df.columns), dtype=c_dtype)
[Timestamp(label) for label in df.columns],
dtype=c_dtype)
elif c_dtype == 'p':
c_dtype = 'O'
col_list = to_datetime(recons.columns)
recons.columns = np.array(
lmap(Timestamp, to_datetime(recons.columns)),
[Timestamp(label) for label in col_list],
dtype=c_dtype)
col_list = df.columns.to_timestamp()
df.columns = np.array(
lmap(Timestamp, df.columns.to_timestamp()),
[Timestamp(label) for label in col_list],
dtype=c_dtype)
else:
c_dtype = type_map.get(c_dtype)
Expand Down
11 changes: 5 additions & 6 deletions pandas/tests/groupby/test_groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import numpy as np
import pytest

from pandas.compat import lmap
from pandas.errors import PerformanceWarning

import pandas as pd
Expand Down Expand Up @@ -874,7 +873,7 @@ def test_mutate_groups():
'cat1': ['a'] * 8 + ['b'] * 6,
'cat2': ['c'] * 2 + ['d'] * 2 + ['e'] * 2 + ['f'] * 2 + ['c'] * 2 +
['d'] * 2 + ['e'] * 2,
'cat3': lmap(lambda x: 'g%s' % x, range(1, 15)),
'cat3': ['g{}'.format(x) for x in range(1, 15)],
'val': np.random.randint(100, size=14),
})

Expand Down Expand Up @@ -1248,17 +1247,17 @@ def test_groupby_sort_multi():
'c': [0, 1, 2],
'd': np.random.randn(3)})

tups = lmap(tuple, df[['a', 'b', 'c']].values)
tups = [tuple(row) for row in df[['a', 'b', 'c']].values]
tups = com.asarray_tuplesafe(tups)
result = df.groupby(['a', 'b', 'c'], sort=True).sum()
tm.assert_numpy_array_equal(result.index.values, tups[[1, 2, 0]])

tups = lmap(tuple, df[['c', 'a', 'b']].values)
tups = [tuple(row) for row in df[['c', 'a', 'b']].values]
tups = com.asarray_tuplesafe(tups)
result = df.groupby(['c', 'a', 'b'], sort=True).sum()
tm.assert_numpy_array_equal(result.index.values, tups)

tups = lmap(tuple, df[['b', 'c', 'a']].values)
tups = [tuple(x) for x in df[['b', 'c', 'a']].values]
tups = com.asarray_tuplesafe(tups)
result = df.groupby(['b', 'c', 'a'], sort=True).sum()
tm.assert_numpy_array_equal(result.index.values, tups[[2, 1, 0]])
Expand All @@ -1270,7 +1269,7 @@ def test_groupby_sort_multi():
result = grouped.sum()

def _check_groupby(df, result, keys, field, f=lambda x: x.sum()):
tups = lmap(tuple, df[keys].values)
tups = [tuple(row) for row in df[keys].values]
tups = com.asarray_tuplesafe(tups)
expected = f(df.groupby(tups)[field])
for k, v in expected.items():
Expand Down
3 changes: 1 addition & 2 deletions pandas/tests/indexes/datetimes/test_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

from pandas._libs import tslib
from pandas._libs.tslibs import iNaT, parsing
from pandas.compat import lmap
from pandas.errors import OutOfBoundsDatetime
import pandas.util._test_decorators as td

Expand Down Expand Up @@ -1264,7 +1263,7 @@ def test_to_datetime_types(self, cache):
# array = ['2012','20120101','20120101 12:01:01']
array = ['20120101', '20120101 12:01:01']
expected = list(to_datetime(array, cache=cache))
result = lmap(Timestamp, array)
result = [Timestamp(date_str) for date_str in array]
tm.assert_almost_equal(result, expected)

# currently fails ###
Expand Down
Loading