Skip to content

Commit 7085b94

Browse files
committed
Merge takluyver/tseries-py3
2 parents de77c4b + 6e1838d commit 7085b94

File tree

9 files changed

+67
-12
lines changed

9 files changed

+67
-12
lines changed

pandas/core/common.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@
1313
from cStringIO import StringIO as BytesIO
1414
import itertools
1515

16+
try:
17+
next
18+
except NameError: # pragma: no cover
19+
# Python < 2.6
20+
def next(x):
21+
return x.next()
22+
1623
from cStringIO import StringIO
1724

1825
from numpy.lib.format import read_array, write_array
@@ -519,7 +526,7 @@ def iterpairs(seq):
519526
# input may not be sliceable
520527
seq_it = iter(seq)
521528
seq_it_next = iter(seq)
522-
_ = seq_it_next.next()
529+
_ = next(seq_it_next)
523530

524531
return itertools.izip(seq_it, seq_it_next)
525532

pandas/core/index.py

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import numpy as np
99

1010
from pandas.util.decorators import cache_readonly
11+
from pandas.util import py3compat
1112
import pandas.core.common as com
1213
import pandas._tseries as lib
1314
import pandas._engines as _gin

pandas/io/parsers.py

+15-5
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@
66
from itertools import izip
77
from urlparse import urlparse
88

9+
try:
10+
next
11+
except NameError: # pragma: no cover
12+
# Python < 2.6
13+
def next(x):
14+
return x.next()
15+
916
import numpy as np
1017

1118
from pandas.core.index import Index, MultiIndex
@@ -489,9 +496,9 @@ def _next_line(self):
489496
raise StopIteration
490497
else:
491498
while self.pos in self.skiprows:
492-
self.data.next()
499+
next(self.data)
493500
self.pos += 1
494-
line = self.data.next()
501+
line = next(self.data)
495502
self.pos += 1
496503
self.buf.append(line)
497504

@@ -682,10 +689,10 @@ def _get_lines(self, rows=None):
682689
try:
683690
if rows is not None:
684691
for _ in xrange(rows):
685-
lines.append(source.next())
692+
lines.append(next(source))
686693
else:
687694
while True:
688-
lines.append(source.next())
695+
lines.append(next(source))
689696
except StopIteration:
690697
if len(lines) == 0:
691698
raise
@@ -757,10 +764,13 @@ def __init__(self, f, colspecs, filler):
757764
assert isinstance(colspec[1], int)
758765

759766
def next(self):
760-
line = self.f.next()
767+
line = next(self.f)
761768
# Note: 'colspecs' is a sequence of half-open intervals.
762769
return [line[fromm:to].strip(self.filler or ' ')
763770
for (fromm, to) in self.colspecs]
771+
772+
# Iterator protocol in Python 3 uses __next__()
773+
__next__ = next
764774

765775

766776
class FixedWidthFieldParser(TextParser):

pandas/src/datetime.pyx

+9-3
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@ ctypedef enum time_res:
3434
r_max = 98
3535
r_invalid = 99
3636

37+
try:
38+
basestring
39+
except NameError: # py3
40+
basestring = str
41+
42+
3743
# Python front end to C extension type _Timestamp
3844
# This serves as the box for datetime64
3945
class Timestamp(_Timestamp):
@@ -1107,12 +1113,12 @@ cpdef int64_t period_asfreq(int64_t period_ordinal, int base1, int64_t mult1,
11071113
cdef:
11081114
int64_t retval
11091115

1110-
if relation not in ('S', 'E'):
1116+
if relation not in (b'S', b'E'):
11111117
raise ValueError('relation argument must be one of S or E')
11121118

11131119
period_ordinal = remove_mult(period_ordinal, mult1)
11141120

1115-
if mult1 != 1 and relation == 'E':
1121+
if mult1 != 1 and relation == b'E':
11161122
period_ordinal += (mult1 - 1)
11171123

11181124
retval = asfreq(period_ordinal, base1, base2, (<char*>relation)[0])
@@ -1130,7 +1136,7 @@ def period_asfreq_arr(ndarray[int64_t] arr, int base1, int64_t mult1, int base2,
11301136
ndarray[int64_t] new_arr
11311137
Py_ssize_t i, sz
11321138

1133-
if relation not in ('S', 'E'):
1139+
if relation not in (b'S', b'E'):
11341140
raise ValueError('relation argument must be one of S or E')
11351141

11361142
sz = len(arr)

pandas/src/np_datetime.c

+17
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,23 @@
1111
#include <numpy/ndarrayobject.h>
1212
#include "np_datetime.h"
1313

14+
#if PY_MAJOR_VERSION >= 3
15+
#define PyIntObject PyLongObject
16+
#define PyInt_Type PyLong_Type
17+
#define PyInt_Check(op) PyLong_Check(op)
18+
#define PyInt_CheckExact(op) PyLong_CheckExact(op)
19+
#define PyInt_FromString PyLong_FromString
20+
#define PyInt_FromUnicode PyLong_FromUnicode
21+
#define PyInt_FromLong PyLong_FromLong
22+
#define PyInt_FromSize_t PyLong_FromSize_t
23+
#define PyInt_FromSsize_t PyLong_FromSsize_t
24+
#define PyInt_AsLong PyLong_AsLong
25+
#define PyInt_AS_LONG PyLong_AS_LONG
26+
#define PyInt_AsSsize_t PyLong_AsSsize_t
27+
#define PyInt_AsUnsignedLongMask PyLong_AsUnsignedLongMask
28+
#define PyInt_AsUnsignedLongLongMask PyLong_AsUnsignedLongLongMask
29+
#endif
30+
1431
/*
1532
* Returns 1 if the given year is a leap year, 0 otherwise.
1633
*/

pandas/tseries/offsets.py

+5
Original file line numberDiff line numberDiff line change
@@ -938,6 +938,11 @@ def __eq__(self, other):
938938
else:
939939
return DateOffset.__eq__(self, other)
940940

941+
# This is identical to DateOffset.__hash__, but has to be redefined here
942+
# for Python 3, because we've redefined __eq__.
943+
def __hash__(self):
944+
return hash(self._params())
945+
941946
def __ne__(self, other):
942947
if isinstance(other, basestring):
943948
from pandas.tseries.frequencies import to_offset

pandas/tseries/period.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import pandas.core.common as com
1010
import pandas.core.datetools as datetools
11+
from pandas.util import py3compat
1112

1213
from pandas._tseries import Timestamp
1314
import pandas._tseries as lib
@@ -185,7 +186,7 @@ def asfreq(self, freq=None, how='E'):
185186
base2, mult2 = _gfc(freq)
186187

187188
new_ordinal = lib.period_asfreq(self.ordinal, base1, mult1,
188-
base2, mult2, how)
189+
base2, mult2, py3compat.str_to_bytes(how))
189190

190191
return Period(new_ordinal, (base2, mult2))
191192

@@ -514,7 +515,7 @@ def __new__(cls, data=None,
514515
base1, mult1 = _gfc(data.freq)
515516
base2, mult2 = _gfc(freq)
516517
data = lib.period_asfreq_arr(data.values, base1, mult1,
517-
base2, mult2, 'E')
518+
base2, mult2, b'E')
518519
else:
519520
if freq is None:
520521
raise ValueError('freq cannot be none')
@@ -558,7 +559,7 @@ def asfreq(self, freq=None, how='E'):
558559

559560
new_data = lib.period_asfreq_arr(self.values,
560561
base1, mult1,
561-
base2, mult2, how)
562+
base2, mult2, py3compat.str_to_bytes(how))
562563

563564
return PeriodIndex(new_data, freq=freq)
564565

pandas/util/py3compat.py

+6
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,16 @@
55
if PY3:
66
def isidentifier(s):
77
return s.isidentifier()
8+
9+
def str_to_bytes(s, encoding='ascii'):
10+
return s.encode(encoding)
811

912
else:
1013
# Python 2
1114
import re
1215
_name_re = re.compile(r"[a-zA-Z_][a-zA-Z0-9_]*$")
1316
def isidentifier(s, dotted=False):
1417
return bool(_name_re.match(s))
18+
19+
def str_to_bytes(s, encoding='ascii'):
20+
return s

setup.py

+2
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@
4040
'zip_safe': False,
4141
'install_requires': ['python-dateutil >= 2',
4242
'numpy >= 1.4'],
43+
'use_2to3_exclude_fixers': ['lib2to3.fixes.fix_next',
44+
],
4345
}
4446
if not _have_setuptools:
4547
sys.exit("need setuptools/distribute for Py3k"

0 commit comments

Comments
 (0)