Skip to content

Commit

Permalink
CLN: Remove miscellaneous rarely used items from pandas.compat (panda…
Browse files Browse the repository at this point in the history
  • Loading branch information
jschendel authored and kiku-jw committed May 16, 2019
1 parent 34a190e commit d484fc6
Show file tree
Hide file tree
Showing 18 changed files with 46 additions and 176 deletions.
9 changes: 4 additions & 5 deletions pandas/_libs/parsers.pyx
@@ -1,9 +1,13 @@
# Copyright (c) 2012, Lambda Foundry, Inc.
# See LICENSE for the license
import bz2
import gzip
import lzma
import os
import sys
import time
import warnings
import zipfile

from csv import QUOTE_MINIMAL, QUOTE_NONNUMERIC, QUOTE_NONE
from errno import ENOENT
Expand Down Expand Up @@ -624,16 +628,13 @@ cdef class TextReader:

if self.compression:
if self.compression == 'gzip':
import gzip
if isinstance(source, basestring):
source = gzip.GzipFile(source, 'rb')
else:
source = gzip.GzipFile(fileobj=source)
elif self.compression == 'bz2':
import bz2
source = bz2.BZ2File(source, 'rb')
elif self.compression == 'zip':
import zipfile
zip_file = zipfile.ZipFile(source)
zip_names = zip_file.namelist()

Expand All @@ -648,8 +649,6 @@ cdef class TextReader:
raise ValueError('Multiple files found in compressed '
'zip file %s', str(zip_names))
elif self.compression == 'xz':
lzma = compat.import_lzma()

if isinstance(source, basestring):
source = lzma.LZMAFile(source, 'rb')
else:
Expand Down
64 changes: 0 additions & 64 deletions pandas/compat/__init__.py
Expand Up @@ -5,7 +5,6 @@
Cross-compatible functions for Python 2 and 3.
Key items to import for 2/3 compatible code:
* iterators: reduce()
* lists: lrange(), lmap(), lzip(), lfilter()
* iterable method compatibility: iteritems, iterkeys, itervalues
* Uses the original method if available, otherwise uses items, keys, values.
Expand Down Expand Up @@ -100,15 +99,6 @@ def signature(f):
'varargs', 'keywords'])
return argspec(args, defaults, varargs, keywords)

def get_range_parameters(data):
"""Gets the start, stop, and step parameters from a range object"""
return data.start, data.stop, data.step

# have to explicitly put builtins into the namespace
intern = sys.intern
reduce = functools.reduce
unichr = chr

# list-producing versions of the major Python iterating functions
def lrange(*args, **kwargs):
return list(range(*args, **kwargs))
Expand All @@ -122,8 +112,6 @@ def lmap(*args, **kwargs):
def lfilter(*args, **kwargs):
return list(filter(*args, **kwargs))

from importlib import reload
reload = reload
Hashable = collections.abc.Hashable
Iterable = collections.abc.Iterable
Iterator = collections.abc.Iterator
Expand All @@ -149,37 +137,12 @@ def bytes_to_str(b, encoding='ascii'):
def signature(f):
return inspect.getargspec(f)

def get_range_parameters(data):
"""Gets the start, stop, and step parameters from a range object"""
# seems we only have indexing ops to infer
# rather than direct accessors
if len(data) > 1:
step = data[1] - data[0]
stop = data[-1] + step
start = data[0]
elif len(data):
start = data[0]
stop = data[0] + 1
step = 1
else:
start = stop = 0
step = 1

return start, stop, step

# import iterator versions of these functions
intern = intern
reduce = reduce
unichr = unichr

# Python 2-builtin ranges produce lists
lrange = builtins.range
lzip = builtins.zip
lmap = builtins.map
lfilter = builtins.filter

reload = builtins.reload

Hashable = collections.Hashable
Iterable = collections.Iterable
Iterator = collections.Iterator
Expand Down Expand Up @@ -247,7 +210,6 @@ class to receive bound method

if PY3:
string_types = str,
class_types = type,
text_type = str
binary_type = bytes

Expand All @@ -274,11 +236,6 @@ def east_asian_len(data, encoding=None, ambiguous_width=1):
else:
return len(data)

def import_lzma():
""" import lzma from the std library """
import lzma
return lzma

def set_function_name(f, name, cls):
""" Bind the name/qualname attributes of the function """
f.__name__ = name
Expand All @@ -289,7 +246,6 @@ def set_function_name(f, name, cls):
return f
else:
string_types = basestring,
class_types = (type, types.ClassType)
text_type = unicode
binary_type = str

Expand Down Expand Up @@ -321,12 +277,6 @@ def east_asian_len(data, encoding=None, ambiguous_width=1):
else:
return len(data)

def import_lzma():
""" import the backported lzma library
or raise ImportError if not available """
from backports import lzma
return lzma

def set_function_name(f, name, cls):
""" Bind the name attributes of the function """
f.__name__ = name
Expand All @@ -335,20 +285,6 @@ def set_function_name(f, name, cls):
string_and_binary_types = string_types + (binary_type,)


if PY2:
# In PY2 functools.wraps doesn't provide metadata pytest needs to generate
# decorated tests using parametrization. See pytest GH issue #2782
def wraps(wrapped, assigned=functools.WRAPPER_ASSIGNMENTS,
updated=functools.WRAPPER_UPDATES):
def wrapper(f):
f = functools.wraps(wrapped, assigned, updated)(f)
f.__wrapped__ = wrapped
return f
return wrapper
else:
wraps = functools.wraps


def add_metaclass(metaclass):
"""Class decorator for creating a class with a metaclass."""
def wrapper(cls):
Expand Down
6 changes: 2 additions & 4 deletions pandas/conftest.py
Expand Up @@ -218,17 +218,15 @@ def all_compare_operators(request):
return request.param


@pytest.fixture(params=[None, 'gzip', 'bz2', 'zip',
pytest.param('xz', marks=td.skip_if_no_lzma)])
@pytest.fixture(params=[None, 'gzip', 'bz2', 'zip', 'xz'])
def compression(request):
"""
Fixture for trying common compression types in compression tests
"""
return request.param


@pytest.fixture(params=['gzip', 'bz2', 'zip',
pytest.param('xz', marks=td.skip_if_no_lzma)])
@pytest.fixture(params=['gzip', 'bz2', 'zip', 'xz'])
def compression_only(request):
"""
Fixture for trying common compression types in compression tests excluding
Expand Down
4 changes: 3 additions & 1 deletion pandas/core/computation/common.py
@@ -1,6 +1,8 @@
from functools import reduce

import numpy as np

from pandas.compat import reduce, string_types
from pandas.compat import string_types

import pandas as pd

Expand Down
4 changes: 2 additions & 2 deletions pandas/core/computation/expr.py
Expand Up @@ -2,14 +2,14 @@
"""

import ast
from functools import partial
from functools import partial, reduce
import itertools as it
import operator
import tokenize

import numpy as np

from pandas.compat import StringIO, lmap, reduce, string_types
from pandas.compat import StringIO, lmap, string_types

import pandas as pd
from pandas import compat
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/indexes/range.py
Expand Up @@ -7,7 +7,7 @@

from pandas._libs import index as libindex, lib
import pandas.compat as compat
from pandas.compat import get_range_parameters, lrange
from pandas.compat import lrange
from pandas.compat.numpy import function as nv
from pandas.util._decorators import Appender, cache_readonly

Expand Down Expand Up @@ -132,7 +132,7 @@ def from_range(cls, data, name=None, dtype=None, **kwargs):
'{0}(...) must be called with object coercible to a '
'range, {1} was passed'.format(cls.__name__, repr(data)))

start, stop, step = get_range_parameters(data)
start, stop, step = data.start, data.stop, data.step
return RangeIndex(start, stop, step, dtype=dtype, name=name, **kwargs)

@classmethod
Expand Down
6 changes: 2 additions & 4 deletions pandas/core/internals/construction.py
Expand Up @@ -10,8 +10,7 @@
from pandas._libs import lib
from pandas._libs.tslibs import IncompatibleFrequency
import pandas.compat as compat
from pandas.compat import (
get_range_parameters, lmap, lrange, raise_with_traceback)
from pandas.compat import lmap, lrange, raise_with_traceback

from pandas.core.dtypes.cast import (
construct_1d_arraylike_from_scalar, construct_1d_ndarray_preserving_na,
Expand Down Expand Up @@ -612,8 +611,7 @@ def sanitize_array(data, index, dtype=None, copy=False,

elif isinstance(data, range):
# GH#16804
start, stop, step = get_range_parameters(data)
arr = np.arange(start, stop, step, dtype='int64')
arr = np.arange(data.start, data.stop, data.step, dtype='int64')
subarr = _try_cast(arr, False, dtype, copy, raise_cast_failure)
else:
subarr = _try_cast(data, False, dtype, copy, raise_cast_failure)
Expand Down
6 changes: 3 additions & 3 deletions pandas/io/common.py
@@ -1,8 +1,11 @@
"""Common IO api utilities"""

import bz2
import codecs
from contextlib import closing, contextmanager
import csv
import gzip
import lzma
import mmap
import os
import zipfile
Expand Down Expand Up @@ -364,15 +367,13 @@ def _get_handle(path_or_buf, mode, encoding=None, compression=None,

# GZ Compression
if compression == 'gzip':
import gzip
if is_path:
f = gzip.open(path_or_buf, mode)
else:
f = gzip.GzipFile(fileobj=path_or_buf)

# BZ Compression
elif compression == 'bz2':
import bz2
if is_path:
f = bz2.BZ2File(path_or_buf, mode)
elif compat.PY2:
Expand Down Expand Up @@ -404,7 +405,6 @@ def _get_handle(path_or_buf, mode, encoding=None, compression=None,

# XZ Compression
elif compression == 'xz':
lzma = compat.import_lzma()
f = lzma.LZMAFile(path_or_buf, mode)

# Unrecognized Compression
Expand Down
3 changes: 1 addition & 2 deletions pandas/io/formats/excel.py
@@ -1,14 +1,13 @@
"""Utilities for conversion to writer-agnostic Excel representation
"""

from functools import reduce
import itertools
import re
import warnings

import numpy as np

from pandas.compat import reduce

from pandas.core.dtypes import missing
from pandas.core.dtypes.common import is_float, is_scalar
from pandas.core.dtypes.generic import ABCMultiIndex, ABCPeriodIndex
Expand Down
6 changes: 3 additions & 3 deletions pandas/io/formats/html.py
Expand Up @@ -8,11 +8,11 @@

from pandas._config import get_option

from pandas.compat import lzip, unichr
from pandas.compat import lzip

from pandas.core.dtypes.generic import ABCMultiIndex

from pandas import compat, option_context
from pandas import option_context

from pandas.io.common import _is_url
from pandas.io.formats.format import TableFormatter, get_level_lengths
Expand Down Expand Up @@ -145,7 +145,7 @@ def render(self):
self._write_table()

if self.should_show_dimensions:
by = chr(215) if compat.PY3 else unichr(215) # ×
by = chr(215)
self.write('<p>{rows} rows {by} {cols} columns</p>'
.format(rows=len(self.frame),
by=by,
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/computation/test_eval.py
@@ -1,4 +1,5 @@
from distutils.version import LooseVersion
from functools import reduce
from itertools import product
import operator
import warnings
Expand All @@ -7,7 +8,6 @@
from numpy.random import rand, randint, randn
import pytest

from pandas.compat import reduce
from pandas.errors import PerformanceWarning
import pandas.util._test_decorators as td

Expand Down
8 changes: 2 additions & 6 deletions pandas/tests/io/parser/test_network.py
Expand Up @@ -19,12 +19,8 @@


@pytest.mark.network
@pytest.mark.parametrize(
"compress_type, extension", [
('gzip', '.gz'), ('bz2', '.bz2'), ('zip', '.zip'),
pytest.param('xz', '.xz', marks=td.skip_if_no_lzma)
]
)
@pytest.mark.parametrize("compress_type, extension", [
('gzip', '.gz'), ('bz2', '.bz2'), ('zip', '.zip'), ('xz', '.xz')])
@pytest.mark.parametrize('mode', ['explicit', 'infer'])
@pytest.mark.parametrize('engine', ['python', 'c'])
def test_compressed_urls(salaries_table, compress_type, extension, mode,
Expand Down
3 changes: 2 additions & 1 deletion pandas/tests/io/test_html.py
@@ -1,4 +1,5 @@
from functools import partial
from importlib import reload
import os
import re
import threading
Expand All @@ -7,7 +8,7 @@
from numpy.random import rand
import pytest

from pandas.compat import BytesIO, StringIO, is_platform_windows, reload
from pandas.compat import BytesIO, StringIO, is_platform_windows
from pandas.errors import ParserError
import pandas.util._test_decorators as td

Expand Down

0 comments on commit d484fc6

Please sign in to comment.