Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1# flake8: noqa 

2 

3__docformat__ = "restructuredtext" 

4 

5# Let users know if they're missing any of our hard dependencies 

6hard_dependencies = ("numpy", "pytz", "dateutil") 

7missing_dependencies = [] 

8 

9for dependency in hard_dependencies: 

10 try: 

11 __import__(dependency) 

12 except ImportError as e: 

13 missing_dependencies.append(f"{dependency}: {e}") 

14 

15if missing_dependencies: 

16 raise ImportError( 

17 "Unable to import required dependencies:\n" + "\n".join(missing_dependencies) 

18 ) 

19del hard_dependencies, dependency, missing_dependencies 

20 

21# numpy compat 

22from pandas.compat.numpy import ( 

23 _np_version_under1p14, 

24 _np_version_under1p15, 

25 _np_version_under1p16, 

26 _np_version_under1p17, 

27 _np_version_under1p18, 

28 _is_numpy_dev, 

29) 

30 

31try: 

32 from pandas._libs import hashtable as _hashtable, lib as _lib, tslib as _tslib 

33except ImportError as e: # pragma: no cover 

34 # hack but overkill to use re 

35 module = str(e).replace("cannot import name ", "") 

36 raise ImportError( 

37 f"C extension: {module} not built. If you want to import " 

38 "pandas from the source directory, you may need to run " 

39 "'python setup.py build_ext --inplace --force' to build " 

40 "the C extensions first." 

41 ) 

42 

43from pandas._config import ( 

44 get_option, 

45 set_option, 

46 reset_option, 

47 describe_option, 

48 option_context, 

49 options, 

50) 

51 

52# let init-time option registration happen 

53import pandas.core.config_init 

54 

55from pandas.core.api import ( 

56 # dtype 

57 Int8Dtype, 

58 Int16Dtype, 

59 Int32Dtype, 

60 Int64Dtype, 

61 UInt8Dtype, 

62 UInt16Dtype, 

63 UInt32Dtype, 

64 UInt64Dtype, 

65 CategoricalDtype, 

66 PeriodDtype, 

67 IntervalDtype, 

68 DatetimeTZDtype, 

69 StringDtype, 

70 BooleanDtype, 

71 # missing 

72 NA, 

73 isna, 

74 isnull, 

75 notna, 

76 notnull, 

77 # indexes 

78 Index, 

79 CategoricalIndex, 

80 Int64Index, 

81 UInt64Index, 

82 RangeIndex, 

83 Float64Index, 

84 MultiIndex, 

85 IntervalIndex, 

86 TimedeltaIndex, 

87 DatetimeIndex, 

88 PeriodIndex, 

89 IndexSlice, 

90 # tseries 

91 NaT, 

92 Period, 

93 period_range, 

94 Timedelta, 

95 timedelta_range, 

96 Timestamp, 

97 date_range, 

98 bdate_range, 

99 Interval, 

100 interval_range, 

101 DateOffset, 

102 # conversion 

103 to_numeric, 

104 to_datetime, 

105 to_timedelta, 

106 # misc 

107 Grouper, 

108 factorize, 

109 unique, 

110 value_counts, 

111 NamedAgg, 

112 array, 

113 Categorical, 

114 set_eng_float_format, 

115 Series, 

116 DataFrame, 

117) 

118 

119from pandas.core.arrays.sparse import SparseDtype 

120 

121from pandas.tseries.api import infer_freq 

122from pandas.tseries import offsets 

123 

124from pandas.core.computation.api import eval 

125 

126from pandas.core.reshape.api import ( 

127 concat, 

128 lreshape, 

129 melt, 

130 wide_to_long, 

131 merge, 

132 merge_asof, 

133 merge_ordered, 

134 crosstab, 

135 pivot, 

136 pivot_table, 

137 get_dummies, 

138 cut, 

139 qcut, 

140) 

141 

142import pandas.api 

143from pandas.util._print_versions import show_versions 

144 

145from pandas.io.api import ( 

146 # excel 

147 ExcelFile, 

148 ExcelWriter, 

149 read_excel, 

150 # parsers 

151 read_csv, 

152 read_fwf, 

153 read_table, 

154 # pickle 

155 read_pickle, 

156 to_pickle, 

157 # pytables 

158 HDFStore, 

159 read_hdf, 

160 # sql 

161 read_sql, 

162 read_sql_query, 

163 read_sql_table, 

164 # misc 

165 read_clipboard, 

166 read_parquet, 

167 read_orc, 

168 read_feather, 

169 read_gbq, 

170 read_html, 

171 read_json, 

172 read_stata, 

173 read_sas, 

174 read_spss, 

175) 

176 

177from pandas.io.json import _json_normalize as json_normalize 

178 

179from pandas.util._tester import test 

180import pandas.testing 

181import pandas.arrays 

182 

183# use the closest tagged version if possible 

184from ._version import get_versions 

185 

186v = get_versions() 

187__version__ = v.get("closest-tag", v["version"]) 

188__git_version__ = v.get("full-revisionid") 

189del get_versions, v 

190 

191# GH 27101 

192# TODO: remove Panel compat in 1.0 

193if pandas.compat.PY37: 

194 

195 def __getattr__(name): 

196 import warnings 

197 

198 if name == "Panel": 

199 

200 warnings.warn( 

201 "The Panel class is removed from pandas. Accessing it " 

202 "from the top-level namespace will also be removed in " 

203 "the next version", 

204 FutureWarning, 

205 stacklevel=2, 

206 ) 

207 

208 class Panel: 

209 pass 

210 

211 return Panel 

212 

213 elif name == "datetime": 

214 warnings.warn( 

215 "The pandas.datetime class is deprecated " 

216 "and will be removed from pandas in a future version. " 

217 "Import from datetime module instead.", 

218 FutureWarning, 

219 stacklevel=2, 

220 ) 

221 

222 from datetime import datetime as dt 

223 

224 return dt 

225 

226 elif name == "np": 

227 

228 warnings.warn( 

229 "The pandas.np module is deprecated " 

230 "and will be removed from pandas in a future version. " 

231 "Import numpy directly instead", 

232 FutureWarning, 

233 stacklevel=2, 

234 ) 

235 import numpy as np 

236 

237 return np 

238 

239 elif name in {"SparseSeries", "SparseDataFrame"}: 

240 warnings.warn( 

241 f"The {name} class is removed from pandas. Accessing it from " 

242 "the top-level namespace will also be removed in the next " 

243 "version", 

244 FutureWarning, 

245 stacklevel=2, 

246 ) 

247 

248 return type(name, (), {}) 

249 

250 elif name == "SparseArray": 

251 

252 warnings.warn( 

253 "The pandas.SparseArray class is deprecated " 

254 "and will be removed from pandas in a future version. " 

255 "Use pandas.arrays.SparseArray instead.", 

256 FutureWarning, 

257 stacklevel=2, 

258 ) 

259 from pandas.core.arrays.sparse import SparseArray as _SparseArray 

260 

261 return _SparseArray 

262 

263 raise AttributeError(f"module 'pandas' has no attribute '{name}'") 

264 

265 

266else: 

267 

268 class Panel: 

269 pass 

270 

271 class SparseDataFrame: 

272 pass 

273 

274 class SparseSeries: 

275 pass 

276 

277 class __numpy: 

278 def __init__(self): 

279 import numpy as np 

280 import warnings 

281 

282 self.np = np 

283 self.warnings = warnings 

284 

285 def __getattr__(self, item): 

286 self.warnings.warn( 

287 "The pandas.np module is deprecated " 

288 "and will be removed from pandas in a future version. " 

289 "Import numpy directly instead", 

290 FutureWarning, 

291 stacklevel=2, 

292 ) 

293 

294 try: 

295 return getattr(self.np, item) 

296 except AttributeError: 

297 raise AttributeError(f"module numpy has no attribute {item}") 

298 

299 np = __numpy() 

300 

301 class __Datetime(type): 

302 

303 from datetime import datetime as dt 

304 

305 datetime = dt 

306 

307 def __getattr__(cls, item): 

308 cls.emit_warning() 

309 

310 try: 

311 return getattr(cls.datetime, item) 

312 except AttributeError: 

313 raise AttributeError(f"module datetime has no attribute {item}") 

314 

315 def __instancecheck__(cls, other): 

316 return isinstance(other, cls.datetime) 

317 

318 class __DatetimeSub(metaclass=__Datetime): 

319 def emit_warning(dummy=0): 

320 import warnings 

321 

322 warnings.warn( 

323 "The pandas.datetime class is deprecated " 

324 "and will be removed from pandas in a future version. " 

325 "Import from datetime instead.", 

326 FutureWarning, 

327 stacklevel=3, 

328 ) 

329 

330 def __new__(cls, *args, **kwargs): 

331 cls.emit_warning() 

332 from datetime import datetime as dt 

333 

334 return dt(*args, **kwargs) 

335 

336 datetime = __DatetimeSub 

337 

338 class __SparseArray(type): 

339 

340 from pandas.core.arrays.sparse import SparseArray as sa 

341 

342 SparseArray = sa 

343 

344 def __instancecheck__(cls, other): 

345 return isinstance(other, cls.SparseArray) 

346 

347 class __SparseArraySub(metaclass=__SparseArray): 

348 def emit_warning(dummy=0): 

349 import warnings 

350 

351 warnings.warn( 

352 "The pandas.SparseArray class is deprecated " 

353 "and will be removed from pandas in a future version. " 

354 "Use pandas.arrays.SparseArray instead.", 

355 FutureWarning, 

356 stacklevel=3, 

357 ) 

358 

359 def __new__(cls, *args, **kwargs): 

360 cls.emit_warning() 

361 from pandas.core.arrays.sparse import SparseArray as sa 

362 

363 return sa(*args, **kwargs) 

364 

365 SparseArray = __SparseArraySub 

366 

367 

368# module level doc-string 

369__doc__ = """ 

370pandas - a powerful data analysis and manipulation library for Python 

371===================================================================== 

372 

373**pandas** is a Python package providing fast, flexible, and expressive data 

374structures designed to make working with "relational" or "labeled" data both 

375easy and intuitive. It aims to be the fundamental high-level building block for 

376doing practical, **real world** data analysis in Python. Additionally, it has 

377the broader goal of becoming **the most powerful and flexible open source data 

378analysis / manipulation tool available in any language**. It is already well on 

379its way toward this goal. 

380 

381Main Features 

382------------- 

383Here are just a few of the things that pandas does well: 

384 

385 - Easy handling of missing data in floating point as well as non-floating 

386 point data. 

387 - Size mutability: columns can be inserted and deleted from DataFrame and 

388 higher dimensional objects 

389 - Automatic and explicit data alignment: objects can be explicitly aligned 

390 to a set of labels, or the user can simply ignore the labels and let 

391 `Series`, `DataFrame`, etc. automatically align the data for you in 

392 computations. 

393 - Powerful, flexible group by functionality to perform split-apply-combine 

394 operations on data sets, for both aggregating and transforming data. 

395 - Make it easy to convert ragged, differently-indexed data in other Python 

396 and NumPy data structures into DataFrame objects. 

397 - Intelligent label-based slicing, fancy indexing, and subsetting of large 

398 data sets. 

399 - Intuitive merging and joining data sets. 

400 - Flexible reshaping and pivoting of data sets. 

401 - Hierarchical labeling of axes (possible to have multiple labels per tick). 

402 - Robust IO tools for loading data from flat files (CSV and delimited), 

403 Excel files, databases, and saving/loading data from the ultrafast HDF5 

404 format. 

405 - Time series-specific functionality: date range generation and frequency 

406 conversion, moving window statistics, date shifting and lagging. 

407"""