Skip to content

Commit e0ebca0

Browse files
committed
CLN: Remove PY2/3 references io directory
xref pandas-devgh-25725
1 parent 882961d commit e0ebca0

23 files changed

+110
-472
lines changed

pandas/core/arrays/categorical.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# pylint: disable=E1101,W0232
22

3+
from shutil import get_terminal_size
34
import textwrap
45
from warnings import warn
56

@@ -38,7 +39,6 @@
3839
from pandas.core.sorting import nargsort
3940

4041
from pandas.io.formats import console
41-
from pandas.io.formats.terminal import get_terminal_size
4242

4343
from .base import ExtensionArray, _extension_array_shared_docs
4444

pandas/core/series.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
Data structure for 1-dimensional cross-sectional and time series data
33
"""
44
from collections import OrderedDict
5+
from shutil import get_terminal_size
56
from textwrap import dedent
67
import warnings
78

@@ -47,7 +48,6 @@
4748
from pandas.core.tools.datetimes import to_datetime
4849

4950
import pandas.io.formats.format as fmt
50-
from pandas.io.formats.terminal import get_terminal_size
5151
import pandas.plotting._core as gfx
5252

5353
# pylint: disable=E1101,E1103

pandas/io/clipboard/clipboards.py

+3-9
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import subprocess
22

3-
from pandas.compat import PY2, text_type
4-
53
from .exceptions import PyperclipException
64

75
EXCEPT_MSG = """
@@ -66,7 +64,7 @@ def copy_qt(text):
6664

6765
def paste_qt():
6866
cb = app.clipboard()
69-
return text_type(cb.text())
67+
return str(cb.text())
7068

7169
return copy_qt, paste_qt
7270

@@ -135,11 +133,7 @@ class ClipboardUnavailable(object):
135133
def __call__(self, *args, **kwargs):
136134
raise PyperclipException(EXCEPT_MSG)
137135

138-
if PY2:
139-
def __nonzero__(self):
140-
return False
141-
else:
142-
def __bool__(self):
143-
return False
136+
def __bool__(self):
137+
return False
144138

145139
return ClipboardUnavailable(), ClipboardUnavailable()

pandas/io/clipboards.py

+11-19
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import warnings
33

44
import pandas.compat as compat
5-
from pandas.compat import PY2, PY3, StringIO
5+
from pandas.compat import StringIO
66

77
from pandas.core.dtypes.generic import ABCDataFrame
88

@@ -36,16 +36,15 @@ def read_clipboard(sep=r'\s+', **kwargs): # pragma: no cover
3636
from pandas.io.parsers import read_csv
3737
text = clipboard_get()
3838

39-
# try to decode (if needed on PY3)
39+
# try to decode (if needed)
4040
# Strange. linux py33 doesn't complain, win py33 does
41-
if PY3:
42-
try:
43-
text = compat.bytes_to_str(
44-
text, encoding=(kwargs.get('encoding') or
45-
get_option('display.encoding'))
46-
)
47-
except AttributeError:
48-
pass
41+
try:
42+
text = compat.bytes_to_str(
43+
text, encoding=(kwargs.get('encoding') or
44+
get_option('display.encoding'))
45+
)
46+
except AttributeError:
47+
pass
4948

5049
# Excel copies into clipboard with \t separation
5150
# inspect no more then the 10 first lines, if they
@@ -75,13 +74,6 @@ def read_clipboard(sep=r'\s+', **kwargs): # pragma: no cover
7574
warnings.warn('read_clipboard with regex separator does not work'
7675
' properly with c engine')
7776

78-
# In PY2, the c table reader first encodes text with UTF-8 but Python
79-
# table reader uses the format of the passed string. For consistency,
80-
# encode strings for python engine so that output from python and c
81-
# engines produce consistent results
82-
if kwargs.get('engine') == 'python' and PY2:
83-
text = text.encode('utf-8')
84-
8577
return read_csv(StringIO(text), sep=sep, **kwargs)
8678

8779

@@ -123,11 +115,11 @@ def to_clipboard(obj, excel=True, sep=None, **kwargs): # pragma: no cover
123115
if sep is None:
124116
sep = '\t'
125117
buf = StringIO()
118+
126119
# clipboard_set (pyperclip) expects unicode
127120
obj.to_csv(buf, sep=sep, encoding='utf-8', **kwargs)
128121
text = buf.getvalue()
129-
if PY2:
130-
text = text.decode('utf-8')
122+
131123
clipboard_set(text)
132124
return
133125
except TypeError:

pandas/io/common.py

+28-145
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,26 @@
22

33
import bz2
44
import codecs
5-
from contextlib import closing, contextmanager
65
import csv
76
import gzip
87
import lzma
8+
from http.client import HTTPException # noqa
99
import mmap
1010
import os
11+
from urllib.error import URLError # noqa
12+
from urllib.parse import ( # noqa
13+
urlencode, urljoin, urlparse as parse_url, uses_netloc, uses_params,
14+
uses_relative)
15+
from urllib.request import pathname2url, urlopen
1116
import zipfile
1217

1318
import pandas.compat as compat
14-
from pandas.compat import BytesIO, StringIO, string_types, text_type
19+
from pandas.compat import BytesIO, string_types, text_type
1520
from pandas.errors import ( # noqa
1621
AbstractMethodError, DtypeWarning, EmptyDataError, ParserError,
1722
ParserWarning)
1823

19-
from pandas.core.dtypes.common import is_file_like, is_number
20-
21-
from pandas.io.formats.printing import pprint_thing
24+
from pandas.core.dtypes.common import is_file_like
2225

2326
# gh-12665: Alias for now and remove later.
2427
CParserError = ParserError
@@ -31,31 +34,6 @@
3134
'-nan', ''}
3235

3336

34-
if compat.PY3:
35-
from urllib.request import urlopen, pathname2url
36-
_urlopen = urlopen
37-
from urllib.parse import urlparse as parse_url
38-
from urllib.parse import (uses_relative, uses_netloc, uses_params,
39-
urlencode, urljoin)
40-
from urllib.error import URLError
41-
from http.client import HTTPException # noqa
42-
else:
43-
from urllib2 import urlopen as _urlopen
44-
from urllib import urlencode, pathname2url # noqa
45-
from urlparse import urlparse as parse_url
46-
from urlparse import uses_relative, uses_netloc, uses_params, urljoin
47-
from urllib2 import URLError # noqa
48-
from httplib import HTTPException # noqa
49-
from contextlib import contextmanager, closing # noqa
50-
from functools import wraps # noqa
51-
52-
# @wraps(_urlopen)
53-
@contextmanager
54-
def urlopen(*args, **kwargs):
55-
with closing(_urlopen(*args, **kwargs)) as f:
56-
yield f
57-
58-
5937
_VALID_URLS = set(uses_relative + uses_netloc + uses_params)
6038
_VALID_URLS.discard('')
6139

@@ -72,10 +50,6 @@ def __next__(self):
7250
raise AbstractMethodError(self)
7351

7452

75-
if not compat.PY3:
76-
BaseIterator.next = lambda self: self.__next__()
77-
78-
7953
def _is_url(url):
8054
"""Check to see if a URL has a valid protocol.
8155
@@ -189,7 +163,8 @@ def get_filepath_or_buffer(filepath_or_buffer, encoding=None,
189163
----------
190164
filepath_or_buffer : a url, filepath (str, py.path.local or pathlib.Path),
191165
or buffer
192-
encoding : the encoding to use to decode py3 bytes, default is 'utf-8'
166+
compression : {{'gzip', 'bz2', 'zip', 'xz', None}}, optional
167+
encoding : the encoding to use to decode bytes, default is 'utf-8'
193168
mode : str, optional
194169
195170
Returns
@@ -202,7 +177,7 @@ def get_filepath_or_buffer(filepath_or_buffer, encoding=None,
202177
filepath_or_buffer = _stringify_path(filepath_or_buffer)
203178

204179
if _is_url(filepath_or_buffer):
205-
req = _urlopen(filepath_or_buffer)
180+
req = urlopen(filepath_or_buffer)
206181
content_encoding = req.headers.get('Content-Encoding', None)
207182
if content_encoding == 'gzip':
208183
# Override compression based on Content-Encoding header
@@ -361,10 +336,6 @@ def _get_handle(path_or_buf, mode, encoding=None, compression=None,
361336

362337
if compression:
363338

364-
if compat.PY2 and not is_path and encoding:
365-
msg = 'compression with encoding is not yet supported in Python 2'
366-
raise ValueError(msg)
367-
368339
# GZ Compression
369340
if compression == 'gzip':
370341
if is_path:
@@ -376,11 +347,6 @@ def _get_handle(path_or_buf, mode, encoding=None, compression=None,
376347
elif compression == 'bz2':
377348
if is_path:
378349
f = bz2.BZ2File(path_or_buf, mode)
379-
elif compat.PY2:
380-
# Python 2's bz2 module can't take file objects, so have to
381-
# run through decompress manually
382-
f = StringIO(bz2.decompress(path_or_buf.read()))
383-
path_or_buf.close()
384350
else:
385351
f = bz2.BZ2File(path_or_buf)
386352

@@ -415,24 +381,19 @@ def _get_handle(path_or_buf, mode, encoding=None, compression=None,
415381
handles.append(f)
416382

417383
elif is_path:
418-
if compat.PY2:
419-
# Python 2
420-
mode = "wb" if mode == "w" else mode
421-
f = open(path_or_buf, mode)
422-
elif encoding:
423-
# Python 3 and encoding
384+
if encoding:
385+
# Encoding
424386
f = open(path_or_buf, mode, encoding=encoding, newline="")
425387
elif is_text:
426-
# Python 3 and no explicit encoding
388+
# No explicit encoding
427389
f = open(path_or_buf, mode, errors='replace', newline="")
428390
else:
429-
# Python 3 and binary mode
391+
# Binary mode
430392
f = open(path_or_buf, mode)
431393
handles.append(f)
432394

433-
# in Python 3, convert BytesIO or fileobjects passed with an encoding
434-
if (compat.PY3 and is_text and
435-
(compression or isinstance(f, need_text_wrapping))):
395+
# Convert BytesIO or file objects passed with an encoding
396+
if is_text and (compression or isinstance(f, need_text_wrapping)):
436397
from io import TextIOWrapper
437398
f = TextIOWrapper(f, encoding=encoding, newline='')
438399
handles.append(f)
@@ -499,11 +460,9 @@ def __iter__(self):
499460
def __next__(self):
500461
newline = self.mmap.readline()
501462

502-
# readline returns bytes, not str, in Python 3,
503-
# but Python's CSV reader expects str, so convert
504-
# the output to str before continuing
505-
if compat.PY3:
506-
newline = compat.bytes_to_str(newline)
463+
# readline returns bytes, not str, but Python's CSV reader
464+
# expects str, so convert the output to str before continuing
465+
newline = compat.bytes_to_str(newline)
507466

508467
# mmap doesn't raise if reading past the allocated
509468
# data but instead returns an empty string, so raise
@@ -513,14 +472,10 @@ def __next__(self):
513472
return newline
514473

515474

516-
if not compat.PY3:
517-
MMapWrapper.next = lambda self: self.__next__()
518-
519-
520475
class UTF8Recoder(BaseIterator):
521476

522477
"""
523-
Iterator that reads an encoded stream and reencodes the input to UTF-8
478+
Iterator that reads an encoded stream and re-encodes the input to UTF-8
524479
"""
525480

526481
def __init__(self, f, encoding):
@@ -536,82 +491,10 @@ def next(self):
536491
return next(self.reader).encode("utf-8")
537492

538493

539-
if compat.PY3: # pragma: no cover
540-
def UnicodeReader(f, dialect=csv.excel, encoding="utf-8", **kwds):
541-
# ignore encoding
542-
return csv.reader(f, dialect=dialect, **kwds)
543-
544-
def UnicodeWriter(f, dialect=csv.excel, encoding="utf-8", **kwds):
545-
return csv.writer(f, dialect=dialect, **kwds)
546-
else:
547-
class UnicodeReader(BaseIterator):
548-
549-
"""
550-
A CSV reader which will iterate over lines in the CSV file "f",
551-
which is encoded in the given encoding.
552-
553-
On Python 3, this is replaced (below) by csv.reader, which handles
554-
unicode.
555-
"""
556-
557-
def __init__(self, f, dialect=csv.excel, encoding="utf-8", **kwds):
558-
f = UTF8Recoder(f, encoding)
559-
self.reader = csv.reader(f, dialect=dialect, **kwds)
560-
561-
def __next__(self):
562-
row = next(self.reader)
563-
return [compat.text_type(s, "utf-8") for s in row]
564-
565-
class UnicodeWriter(object):
566-
567-
"""
568-
A CSV writer which will write rows to CSV file "f",
569-
which is encoded in the given encoding.
570-
"""
571-
572-
def __init__(self, f, dialect=csv.excel, encoding="utf-8", **kwds):
573-
# Redirect output to a queue
574-
self.queue = StringIO()
575-
self.writer = csv.writer(self.queue, dialect=dialect, **kwds)
576-
self.stream = f
577-
self.encoder = codecs.getincrementalencoder(encoding)()
578-
self.quoting = kwds.get("quoting", None)
579-
580-
def writerow(self, row):
581-
def _check_as_is(x):
582-
return (self.quoting == csv.QUOTE_NONNUMERIC and
583-
is_number(x)) or isinstance(x, str)
584-
585-
row = [x if _check_as_is(x)
586-
else pprint_thing(x).encode("utf-8") for x in row]
587-
588-
self.writer.writerow([s for s in row])
589-
# Fetch UTF-8 output from the queue ...
590-
data = self.queue.getvalue()
591-
data = data.decode("utf-8")
592-
# ... and re-encode it into the target encoding
593-
data = self.encoder.encode(data)
594-
# write to the target stream
595-
self.stream.write(data)
596-
# empty queue
597-
self.queue.truncate(0)
598-
599-
def writerows(self, rows):
600-
def _check_as_is(x):
601-
return (self.quoting == csv.QUOTE_NONNUMERIC and
602-
is_number(x)) or isinstance(x, str)
603-
604-
for i, row in enumerate(rows):
605-
rows[i] = [x if _check_as_is(x)
606-
else pprint_thing(x).encode("utf-8") for x in row]
607-
608-
self.writer.writerows([[s for s in row] for row in rows])
609-
# Fetch UTF-8 output from the queue ...
610-
data = self.queue.getvalue()
611-
data = data.decode("utf-8")
612-
# ... and re-encode it into the target encoding
613-
data = self.encoder.encode(data)
614-
# write to the target stream
615-
self.stream.write(data)
616-
# empty queue
617-
self.queue.truncate(0)
494+
def UnicodeReader(f, dialect=csv.excel, encoding="utf-8", **kwds):
495+
# ignore encoding
496+
return csv.reader(f, dialect=dialect, **kwds)
497+
498+
499+
def UnicodeWriter(f, dialect=csv.excel, encoding="utf-8", **kwds):
500+
return csv.writer(f, dialect=dialect, **kwds)

0 commit comments

Comments
 (0)