Skip to content

Commit 648ae4f

Browse files
committed
BLD: consolidate remaining extensions
moves extensions to pandas/_libs, which holds the extension code and also the generated builds (as its importable). pandas/_libs/src is now almost an includes dir, holding low-frequency changing code. This consolidates the import process making it more uniform and consistent throughout the codebase. Finally this cleans up the remaining top-level namespace (with some deprecations in place for example pandas.lib, pandas.tslib, pandas.json, pandas.parser. I listed all of the changes in the whatsnew, but I don't think worthwhile deprecating anything else. Author: Jeff Reback <[email protected]> Closes pandas-dev#15537 from jreback/extensions3 and squashes the following commits: a6d6cfa [Jeff Reback] BLD: rename / move some extensions
1 parent c52ff68 commit 648ae4f

File tree

243 files changed

+885
-771
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

243 files changed

+885
-771
lines changed

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
tseries: pandas/lib.pyx pandas/tslib.pyx pandas/hashtable.pyx
1+
tseries: pandas/_libs/lib.pyx pandas/_libs/tslib.pyx pandas/_libs/hashtable.pyx
22
python setup.py build_ext --inplace
33

44
.PHONY : develop build clean clean_pyc tseries doc

asv_bench/benchmarks/binary_ops.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -107,4 +107,4 @@ def setup(self):
107107
self.s = Series(date_range('20010101', periods=self.N, freq='T', tz='US/Eastern'))
108108
self.ts = self.s[self.halfway]
109109

110-
self.s2 = Series(date_range('20010101', periods=self.N, freq='s', tz='US/Eastern'))
110+
self.s2 = Series(date_range('20010101', periods=self.N, freq='s', tz='US/Eastern'))

asv_bench/benchmarks/pandas_vb_common.py

+10-4
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,22 @@
88
import random
99
import numpy as np
1010
import threading
11+
from importlib import import_module
12+
1113
try:
1214
from pandas.compat import range
1315
except ImportError:
1416
pass
1517

1618
np.random.seed(1234)
17-
try:
18-
import pandas._tseries as lib
19-
except:
20-
import pandas.lib as lib
19+
20+
# try em until it works!
21+
for imp in ['pandas_tseries', 'pandas.lib', 'pandas._libs.lib']:
22+
try:
23+
lib = import_module(imp)
24+
break
25+
except:
26+
pass
2127

2228
try:
2329
Panel = Panel

asv_bench/benchmarks/panel_methods.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,4 @@ def time_shift(self):
2121
self.panel.shift(1)
2222

2323
def time_shift_minor(self):
24-
self.panel.shift(1, axis='minor')
24+
self.panel.shift(1, axis='minor')

doc/source/whatsnew/v0.20.0.txt

+29
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,35 @@ New Behavior:
484484
In [11]: index.memory_usage(deep=True)
485485
Out[11]: 260
486486

487+
.. _whatsnew_0200.api_breaking.extensions:
488+
489+
Extension Modules Moved
490+
^^^^^^^^^^^^^^^^^^^^^^^
491+
492+
Some formerly public c/c++/cython extension modules have been moved and/or renamed. These are all removed from the public API.
493+
If indicated, a deprecation warning will be issued if you reference that module. (:issue:`12588`)
494+
495+
.. csv-table::
496+
:header: "Previous Location", "New Location", "Deprecated"
497+
:widths: 30, 30, 4
498+
499+
"pandas.lib", "pandas._libs.lib", "X"
500+
"pandas.tslib", "pandas._libs.tslib", "X"
501+
"pandas._join", "pandas._libs.join", ""
502+
"pandas._period", "pandas._libs.period", ""
503+
"pandas.msgpack", "pandas.io.msgpack", ""
504+
"pandas.index", "pandas._libs.index", ""
505+
"pandas.algos", "pandas._libs.algos", ""
506+
"pandas.hashtable", "pandas._libs.hashtable", ""
507+
"pandas.json", "pandas.io.json.libjson", "X"
508+
"pandas.parser", "pandas.io.libparsers", "X"
509+
"pandas.io.sas.saslib", "pandas.io.sas.libsas", ""
510+
"pandas._testing", "pandas.util.libtesting", ""
511+
"pandas._sparse", "pandas.sparse.libsparse", ""
512+
"pandas._hash", "pandas.tools.libhash", ""
513+
"pandas._window", "pandas.core.libwindow", ""
514+
515+
487516
.. _whatsnew_0200.api_breaking.groupby_describe:
488517

489518
Groupby Describe Formatting

pandas/__init__.py

+11-3
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@
2323
from pandas.compat.numpy import *
2424

2525
try:
26-
from pandas import hashtable, tslib, lib
26+
from pandas._libs import (hashtable as _hashtable,
27+
lib as _lib,
28+
tslib as _tslib)
2729
except ImportError as e: # pragma: no cover
2830
# hack but overkill to use re
2931
module = str(e).lstrip('cannot import name ')
@@ -52,11 +54,17 @@
5254
from pandas.tools.util import to_numeric
5355
from pandas.core.reshape import melt
5456
from pandas.util.print_versions import show_versions
55-
5657
from pandas.io.api import *
57-
5858
from pandas.util._tester import test
5959

60+
# extension module deprecations
61+
from pandas.util.depr_module import _DeprecatedModule
62+
63+
json = _DeprecatedModule(deprmod='pandas.json', deprmodto='pandas.io.json.libjson')
64+
parser = _DeprecatedModule(deprmod='pandas.parser', deprmodto='pandas.io.libparsers')
65+
lib = _DeprecatedModule(deprmod='pandas.lib', deprmodto='pandas._libs.lib')
66+
tslib = _DeprecatedModule(deprmod='pandas.tslib', deprmodto='pandas._libs.tslib')
67+
6068
# use the closest tagged version if possible
6169
from ._version import get_versions
6270
v = get_versions()

pandas/_libs/__init__.py

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# flake8: noqa
2+
3+
from .tslib import iNaT, NaT, Timestamp, Timedelta, OutOfBoundsDatetime
4+
5+
# TODO
6+
# period is directly dependent on tslib and imports python
7+
# modules, so exposing Period as an alias is currently not possible
8+
# from period import Period

pandas/algos.pyx renamed to pandas/_libs/algos.pyx

+2-2
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ float64 = np.dtype(np.float64)
3737
cdef double NaN = <double> np.NaN
3838
cdef double nan = NaN
3939

40-
cdef extern from "src/headers/math.h":
40+
cdef extern from "../src/headers/math.h":
4141
double sqrt(double x) nogil
4242
double fabs(double) nogil
4343

@@ -46,7 +46,7 @@ from util cimport numeric, get_nat
4646

4747
cimport lib
4848
from lib cimport is_null_datetimelike
49-
from pandas import lib
49+
from pandas._libs import lib
5050

5151
cdef int64_t iNaT = get_nat()
5252

pandas/src/algos_common_helper.pxi.in renamed to pandas/_libs/algos_common_helper.pxi.in

+1-1
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,7 @@ def arrmap_{{name}}(ndarray[{{c_type}}] index, object func):
433433

434434
cdef ndarray[object] result = np.empty(length, dtype=np.object_)
435435

436-
from pandas.lib import maybe_convert_objects
436+
from pandas._libs.lib import maybe_convert_objects
437437

438438
for i in range(length):
439439
result[i] = func(index[i])
File renamed without changes.

pandas/hashtable.pyx renamed to pandas/_libs/hashtable.pyx

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ cdef extern from "numpy/npy_math.h":
2222
cimport cython
2323
cimport numpy as cnp
2424

25-
from pandas.lib import checknull
25+
from pandas._libs.lib import checknull
2626

2727
cnp.import_array()
2828
cnp.import_ufunc()

pandas/index.pyx renamed to pandas/_libs/index.pyx

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ import numpy as np
1717

1818
cimport tslib
1919
from hashtable cimport *
20-
from pandas import algos, tslib, hashtable as _hash
21-
from pandas.tslib import Timestamp, Timedelta
20+
from pandas._libs import tslib, algos, hashtable as _hash
21+
from pandas._libs.tslib import Timestamp, Timedelta
2222

2323
from datetime cimport (get_datetime64_value, _pydatetime_to_dts,
2424
pandas_datetimestruct)

pandas/src/join.pyx renamed to pandas/_libs/join.pyx

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@ float64 = np.dtype(np.float64)
3232
cdef double NaN = <double> np.NaN
3333
cdef double nan = NaN
3434

35-
from pandas.algos import groupsort_indexer, ensure_platform_int
35+
from pandas._libs.algos import groupsort_indexer, ensure_platform_int
3636
from pandas.core.algorithms import take_nd
3737

38-
include "joins_func_helper.pxi"
38+
include "join_func_helper.pxi"
3939

4040

4141
def inner_join(ndarray[int64_t] left, ndarray[int64_t] right,
File renamed without changes.
File renamed without changes.
File renamed without changes.

pandas/src/period.pyx renamed to pandas/_libs/period.pyx

+19-25
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,15 @@ cdef extern from "datetime_helper.h":
1616
from libc.stdlib cimport free
1717

1818
from pandas import compat
19-
20-
from pandas.tseries import offsets
21-
from pandas.tseries.tools import parse_time_string
19+
from pandas.compat import PY2
2220

2321
cimport cython
2422
from datetime cimport *
25-
cimport util
26-
cimport lib
23+
cimport util, lib
2724
from lib cimport is_null_datetimelike, is_period
28-
import lib
29-
from pandas import tslib
30-
from tslib import Timedelta, Timestamp, iNaT, NaT
31-
from tslib import have_pytz, _get_utcoffset
25+
from pandas._libs import tslib, lib
26+
from pandas._libs.tslib import (Timedelta, Timestamp, iNaT,
27+
NaT, have_pytz, _get_utcoffset)
3228
from tslib cimport (
3329
maybe_get_tz,
3430
_is_utc,
@@ -37,12 +33,10 @@ from tslib cimport (
3733
_nat_scalar_rules,
3834
)
3935

36+
from pandas.tseries import offsets
37+
from pandas.tseries.tools import parse_time_string
4038
from pandas.tseries import frequencies
4139

42-
from sys import version_info
43-
44-
cdef bint PY2 = version_info[0] == 2
45-
4640
cdef int64_t NPY_NAT = util.get_nat()
4741

4842
cdef int RESO_US = frequencies.RESO_US
@@ -474,7 +468,7 @@ def extract_ordinals(ndarray[object] values, freq):
474468
p = values[i]
475469

476470
if is_null_datetimelike(p):
477-
ordinals[i] = tslib.iNaT
471+
ordinals[i] = iNaT
478472
else:
479473
try:
480474
ordinals[i] = p.ordinal
@@ -485,9 +479,9 @@ def extract_ordinals(ndarray[object] values, freq):
485479

486480
except AttributeError:
487481
p = Period(p, freq=freq)
488-
if p is tslib.NaT:
482+
if p is NaT:
489483
# input may contain NaT-like string
490-
ordinals[i] = tslib.iNaT
484+
ordinals[i] = iNaT
491485
else:
492486
ordinals[i] = p.ordinal
493487

@@ -716,8 +710,8 @@ cdef class _Period(object):
716710
"""
717711
Fast creation from an ordinal and freq that are already validated!
718712
"""
719-
if ordinal == tslib.iNaT:
720-
return tslib.NaT
713+
if ordinal == iNaT:
714+
return NaT
721715
else:
722716
self = _Period.__new__(cls)
723717
self.ordinal = ordinal
@@ -730,7 +724,7 @@ cdef class _Period(object):
730724
msg = _DIFFERENT_FREQ.format(self.freqstr, other.freqstr)
731725
raise IncompatibleFrequency(msg)
732726
return PyObject_RichCompareBool(self.ordinal, other.ordinal, op)
733-
elif other is tslib.NaT:
727+
elif other is NaT:
734728
return _nat_scalar_rules[op]
735729
# index/series like
736730
elif hasattr(other, '_typ'):
@@ -776,8 +770,8 @@ cdef class _Period(object):
776770
offsets.Tick, offsets.DateOffset,
777771
Timedelta)):
778772
return self._add_delta(other)
779-
elif other is tslib.NaT:
780-
return tslib.NaT
773+
elif other is NaT:
774+
return NaT
781775
elif lib.is_integer(other):
782776
ordinal = self.ordinal + other * self.freq.n
783777
return Period(ordinal=ordinal, freq=self.freq)
@@ -808,8 +802,8 @@ cdef class _Period(object):
808802
else: # pragma: no cover
809803
return NotImplemented
810804
elif isinstance(other, Period):
811-
if self is tslib.NaT:
812-
return tslib.NaT
805+
if self is NaT:
806+
return NaT
813807
return NotImplemented
814808
else:
815809
return NotImplemented
@@ -1164,7 +1158,7 @@ class Period(_Period):
11641158
if (year is None and month is None and
11651159
quarter is None and day is None and
11661160
hour is None and minute is None and second is None):
1167-
ordinal = tslib.iNaT
1161+
ordinal = iNaT
11681162
else:
11691163
if freq is None:
11701164
raise ValueError("If value is None, freq cannot be None")
@@ -1190,7 +1184,7 @@ class Period(_Period):
11901184
ordinal = converted.ordinal
11911185

11921186
elif is_null_datetimelike(value) or value in tslib._nat_strings:
1193-
ordinal = tslib.iNaT
1187+
ordinal = iNaT
11941188

11951189
elif isinstance(value, compat.string_types) or lib.is_integer(value):
11961190
if lib.is_integer(value):
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

pandas/src/ujson/python/objToJSON.c renamed to pandas/_libs/src/ujson/python/objToJSON.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ void initObjToJSON(void)
180180
Py_DECREF(mod_pandas);
181181
}
182182

183-
mod_tslib = PyImport_ImportModule("pandas.tslib");
183+
mod_tslib = PyImport_ImportModule("pandas._libs.tslib");
184184
if (mod_tslib) {
185185
cls_nat = (PyTypeObject *)PyObject_GetAttrString(mod_tslib, "NaTType");
186186
Py_DECREF(mod_tslib);

pandas/src/ujson/python/ujson.c renamed to pandas/_libs/src/ujson/python/ujson.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ static PyMethodDef ujsonMethods[] = {
8080

8181
static struct PyModuleDef moduledef = {
8282
PyModuleDef_HEAD_INIT,
83-
"_pandasujson",
83+
"_libjson",
8484
0, /* m_doc */
8585
-1, /* m_size */
8686
ujsonMethods, /* m_methods */
@@ -90,14 +90,14 @@ static struct PyModuleDef moduledef = {
9090
NULL /* m_free */
9191
};
9292

93-
#define PYMODINITFUNC PyMODINIT_FUNC PyInit_json(void)
93+
#define PYMODINITFUNC PyMODINIT_FUNC PyInit_libjson(void)
9494
#define PYMODULE_CREATE() PyModule_Create(&moduledef)
9595
#define MODINITERROR return NULL
9696

9797
#else
9898

99-
#define PYMODINITFUNC PyMODINIT_FUNC initjson(void)
100-
#define PYMODULE_CREATE() Py_InitModule("json", ujsonMethods)
99+
#define PYMODINITFUNC PyMODINIT_FUNC initlibjson(void)
100+
#define PYMODULE_CREATE() Py_InitModule("libjson", ujsonMethods)
101101
#define MODINITERROR return
102102

103103
#endif
File renamed without changes.
File renamed without changes.
File renamed without changes.

pandas/compat/pickle_compat.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,13 @@ def load_reduce(self):
6262

6363
# 10890
6464
('pandas.core.series', 'TimeSeries'): ('pandas.core.series', 'Series'),
65-
('pandas.sparse.series', 'SparseTimeSeries'): ('pandas.sparse.series', 'SparseSeries')
65+
('pandas.sparse.series', 'SparseTimeSeries'): ('pandas.sparse.series', 'SparseSeries'),
66+
67+
# 12588, extensions moving
68+
('pandas._sparse', 'BlockIndex'): ('pandas.sparse.libsparse', 'BlockIndex'),
69+
('pandas.tslib', 'Timestamp'): ('pandas._libs.tslib', 'Timestamp'),
70+
('pandas.tslib', '__nat_unpickle'): ('pandas._libs.tslib', '__nat_unpickle'),
71+
('pandas._period', 'Period'): ('pandas._libs.period', 'Period')
6672
}
6773

6874

pandas/computation/scope.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
"""Module for scope operations
1+
"""
2+
Module for scope operations
23
"""
34

45
import sys
@@ -10,7 +11,8 @@
1011

1112
import numpy as np
1213

13-
import pandas as pd
14+
import pandas
15+
import pandas as pd # noqa
1416
from pandas.compat import DeepChainMap, map, StringIO
1517
from pandas.core.base import StringMixin
1618
import pandas.computation as compu
@@ -46,7 +48,7 @@ def _raw_hex_id(obj):
4648

4749

4850
_DEFAULT_GLOBALS = {
49-
'Timestamp': pd.lib.Timestamp,
51+
'Timestamp': pandas._libs.lib.Timestamp,
5052
'datetime': datetime.datetime,
5153
'True': True,
5254
'False': False,

0 commit comments

Comments
 (0)