Skip to content

Commit 62243c1

Browse files
committed
ENH: add general save/load functions to pandas namespace
1 parent cb81978 commit 62243c1

File tree

3 files changed

+52
-22
lines changed

3 files changed

+52
-22
lines changed

pandas/core/api.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from pandas.core.datetools import DateOffset
66
import pandas.core.datetools as datetools
77

8-
from pandas.core.common import isnull, notnull, set_printoptions
8+
from pandas.core.common import isnull, notnull, set_printoptions, save, load
99
from pandas.core.index import Index, Int64Index, Factor, MultiIndex
1010
from pandas.core.daterange import DateRange
1111
from pandas.core.series import Series, TimeSeries
@@ -17,3 +17,4 @@
1717

1818
DataMatrix = DataFrame
1919
WidePanel = Panel
20+

pandas/core/common.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""
22
Misc tools for implementing data structures
33
"""
4+
import cPickle
45
try:
56
from io import BytesIO
67
except ImportError: # Python < 2.6
@@ -532,3 +533,40 @@ def _asarray_tuplesafe(values, dtype=None):
532533

533534
return result
534535

536+
537+
def save(obj, path):
538+
"""
539+
Pickle (serialize) object to input file path
540+
541+
Parameters
542+
----------
543+
obj : any object
544+
path : string
545+
File path
546+
"""
547+
f = open(path, 'wb')
548+
try:
549+
cPickle.dump(obj, f, protocol=cPickle.HIGHEST_PROTOCOL)
550+
finally:
551+
f.close()
552+
553+
554+
def load(path):
555+
"""
556+
Load pickled pandas object (or any other pickled object) from the specified
557+
file path
558+
559+
Parameters
560+
----------
561+
path : string
562+
File path
563+
564+
Returns
565+
-------
566+
unpickled : type of object stored in file
567+
"""
568+
f = open(path, 'rb')
569+
try:
570+
return cPickle.load(f)
571+
finally:
572+
f.close()

pandas/core/generic.py

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import numpy as np
22
import cPickle
33

4+
from pandas.core.common import save, load
45
from pandas.core.index import Index, MultiIndex, _ensure_index
56
import pandas.core.datetools as datetools
67

@@ -9,20 +10,12 @@
910

1011
class Picklable(object):
1112

12-
def save(self, fileName):
13-
f = open(fileName, 'wb')
14-
try:
15-
cPickle.dump(self, f, protocol=cPickle.HIGHEST_PROTOCOL)
16-
finally:
17-
f.close()
13+
def save(self, path):
14+
save(self, path)
1815

1916
@classmethod
20-
def load(cls, fileName):
21-
f = open(fileName, 'rb')
22-
try:
23-
return cPickle.load(f)
24-
finally:
25-
f.close()
17+
def load(cls, path):
18+
return load(path)
2619

2720
class PandasError(Exception):
2821
pass
@@ -461,26 +454,24 @@ def add_suffix(self, suffix):
461454

462455
def rename_axis(self, mapper, axis=0, copy=True):
463456
"""
464-
Alter index and / or columns using input function or
465-
functions. Function / dict values must be unique (1-to-1). Labels not
466-
contained in a dict / Series will be left as-is.
457+
Alter index and / or columns using input function or functions.
458+
Function / dict values must be unique (1-to-1). Labels not contained in
459+
a dict / Series will be left as-is.
467460
468461
Parameters
469462
----------
470-
index : dict-like or function, optional
471-
Transformation to apply to index values
472-
columns : dict-like or function, optional
473-
Transformation to apply to column values
463+
mapper : dict-like or function, optional
464+
axis : int, default 0
474465
copy : boolean, default True
475466
Also copy underlying data
476467
477468
See also
478469
--------
479-
Series.rename
470+
DataFrame.rename
480471
481472
Returns
482473
-------
483-
renamed : DataFrame (new object)
474+
renamed : type of caller
484475
"""
485476
# should move this at some point
486477
from pandas.core.series import _get_rename_function

0 commit comments

Comments
 (0)