Skip to content

Commit 8f7b01e

Browse files
committed
CLN: Remove PY2/3 references io directory
xref gh-25725
1 parent d404460 commit 8f7b01e

17 files changed

+102
-329
lines changed

pandas/io/clipboard/clipboards.py

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

3-
from pandas.compat import PY2, text_type
3+
from pandas.compat import text_type
44

55
from .exceptions import PyperclipException
66

@@ -135,11 +135,7 @@ class ClipboardUnavailable(object):
135135
def __call__(self, *args, **kwargs):
136136
raise PyperclipException(EXCEPT_MSG)
137137

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

145141
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-142
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,24 @@
11
"""Common IO api utilities"""
22

33
import codecs
4-
from contextlib import closing, contextmanager
54
import csv
5+
from http.client import HTTPException # noqa
66
import mmap
77
import os
8+
from urllib.error import URLError # noqa
9+
from urllib.parse import ( # noqa
10+
urlencode, urljoin, urlparse as parse_url, uses_netloc, uses_params,
11+
uses_relative)
12+
from urllib.request import pathname2url, urlopen
813
import zipfile
914

1015
import pandas.compat as compat
11-
from pandas.compat import BytesIO, StringIO, string_types, text_type
16+
from pandas.compat import BytesIO, string_types, text_type
1217
from pandas.errors import ( # noqa
1318
AbstractMethodError, DtypeWarning, EmptyDataError, ParserError,
1419
ParserWarning)
1520

16-
from pandas.core.dtypes.common import is_file_like, is_number
17-
18-
from pandas.io.formats.printing import pprint_thing
21+
from pandas.core.dtypes.common import is_file_like
1922

2023
# gh-12665: Alias for now and remove later.
2124
CParserError = ParserError
@@ -28,29 +31,7 @@
2831
'-nan', ''}
2932

3033

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

5536

5637
_VALID_URLS = set(uses_relative + uses_netloc + uses_params)
@@ -69,10 +50,6 @@ def __next__(self):
6950
raise AbstractMethodError(self)
7051

7152

72-
if not compat.PY3:
73-
BaseIterator.next = lambda self: self.__next__()
74-
75-
7653
def _is_url(url):
7754
"""Check to see if a URL has a valid protocol.
7855
@@ -186,7 +163,8 @@ def get_filepath_or_buffer(filepath_or_buffer, encoding=None,
186163
----------
187164
filepath_or_buffer : a url, filepath (str, py.path.local or pathlib.Path),
188165
or buffer
189-
encoding : the encoding to use to decode py3 bytes, default is 'utf-8'
166+
compression : str, optional
167+
encoding : the encoding to use to decode bytes, default is 'utf-8'
190168
mode : str, optional
191169
192170
Returns
@@ -358,10 +336,6 @@ def _get_handle(path_or_buf, mode, encoding=None, compression=None,
358336

359337
if compression:
360338

361-
if compat.PY2 and not is_path and encoding:
362-
msg = 'compression with encoding is not yet supported in Python 2'
363-
raise ValueError(msg)
364-
365339
# GZ Compression
366340
if compression == 'gzip':
367341
import gzip
@@ -375,11 +349,6 @@ def _get_handle(path_or_buf, mode, encoding=None, compression=None,
375349
import bz2
376350
if is_path:
377351
f = bz2.BZ2File(path_or_buf, mode)
378-
elif compat.PY2:
379-
# Python 2's bz2 module can't take file objects, so have to
380-
# run through decompress manually
381-
f = StringIO(bz2.decompress(path_or_buf.read()))
382-
path_or_buf.close()
383352
else:
384353
f = bz2.BZ2File(path_or_buf)
385354

@@ -415,24 +384,19 @@ def _get_handle(path_or_buf, mode, encoding=None, compression=None,
415384
handles.append(f)
416385

417386
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
387+
if encoding:
388+
# Encoding
424389
f = open(path_or_buf, mode, encoding=encoding, newline="")
425390
elif is_text:
426-
# Python 3 and no explicit encoding
391+
# No explicit encoding
427392
f = open(path_or_buf, mode, errors='replace', newline="")
428393
else:
429-
# Python 3 and binary mode
394+
# Binary mode
430395
f = open(path_or_buf, mode)
431396
handles.append(f)
432397

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))):
398+
# Convert BytesIO or file objects passed with an encoding
399+
if is_text and (compression or isinstance(f, need_text_wrapping)):
436400
from io import TextIOWrapper
437401
f = TextIOWrapper(f, encoding=encoding, newline='')
438402
handles.append(f)
@@ -499,11 +463,9 @@ def __iter__(self):
499463
def __next__(self):
500464
newline = self.mmap.readline()
501465

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)
466+
# readline returns bytes, not str, but Python's CSV reader
467+
# expects str, so convert the output to str before continuing
468+
newline = compat.bytes_to_str(newline)
507469

508470
# mmap doesn't raise if reading past the allocated
509471
# data but instead returns an empty string, so raise
@@ -513,14 +475,10 @@ def __next__(self):
513475
return newline
514476

515477

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

522480
"""
523-
Iterator that reads an encoded stream and reencodes the input to UTF-8
481+
Iterator that reads an encoded stream and re-encodes the input to UTF-8
524482
"""
525483

526484
def __init__(self, f, encoding):
@@ -536,82 +494,10 @@ def next(self):
536494
return next(self.reader).encode("utf-8")
537495

538496

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)
497+
def UnicodeReader(f, dialect=csv.excel, encoding="utf-8", **kwds):
498+
# ignore encoding
499+
return csv.reader(f, dialect=dialect, **kwds)
500+
501+
502+
def UnicodeWriter(f, dialect=csv.excel, encoding="utf-8", **kwds):
503+
return csv.writer(f, dialect=dialect, **kwds)

pandas/io/excel/_base.py

+2-5
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919

2020
from pandas.io.common import _NA_VALUES, _stringify_path, _validate_header_arg
2121
from pandas.io.excel._util import (
22-
_fill_mi_header, _get_default_writer, _maybe_convert_to_string,
23-
_maybe_convert_usecols, _pop_header_name, get_writer)
22+
_fill_mi_header, _get_default_writer, _maybe_convert_usecols,
23+
_pop_header_name, get_writer)
2424
from pandas.io.formats.printing import pprint_thing
2525
from pandas.io.parsers import TextParser
2626

@@ -476,9 +476,6 @@ def parse(self,
476476
if header_names:
477477
output[asheetname].columns = output[
478478
asheetname].columns.set_names(header_names)
479-
elif compat.PY2:
480-
output[asheetname].columns = _maybe_convert_to_string(
481-
output[asheetname].columns)
482479

483480
except EmptyDataError:
484481
# No Data, return an empty DataFrame

0 commit comments

Comments
 (0)