Python pandas.compat() Examples
The following are 30
code examples of pandas.compat().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
pandas
, or try the search function
.
Example #1
Source File: test_packers.py From vnpy_crypto with MIT License | 6 votes |
def test_msgpacks_legacy(self, current_packers_data, all_packers_data, legacy_packer, datapath): version = os.path.basename(os.path.dirname(legacy_packer)) # GH12142 0.17 files packed in P2 can't be read in P3 if (compat.PY3 and version.startswith('0.17.') and legacy_packer.split('.')[-4][-1] == '2'): msg = "Files packed in Py2 can't be read in Py3 ({})" pytest.skip(msg.format(version)) try: with catch_warnings(record=True): self.compare(current_packers_data, all_packers_data, legacy_packer, version) except ImportError: # blosc not installed pass
Example #2
Source File: test_packers.py From predictive-maintenance-using-machine-learning with Apache License 2.0 | 6 votes |
def setup_method(self, method): super(TestEncoding, self).setup_method(method) data = { 'A': [compat.u('\u2019')] * 1000, 'B': np.arange(1000, dtype=np.int32), 'C': list(100 * 'abcdefghij'), 'D': date_range(datetime.datetime(2015, 4, 1), periods=1000), 'E': [datetime.timedelta(days=x) for x in range(1000)], 'G': [400] * 1000 } self.frame = { 'float': DataFrame({k: data[k] for k in ['A', 'A']}), 'int': DataFrame({k: data[k] for k in ['B', 'B']}), 'mixed': DataFrame(data), } self.utf_encodings = ['utf8', 'utf16', 'utf32']
Example #3
Source File: generate_legacy_storage_files.py From predictive-maintenance-using-machine-learning with Apache License 2.0 | 6 votes |
def write_legacy_pickles(output_dir): # make sure we are < 0.13 compat (in py3) try: from pandas.compat import zip, cPickle as pickle # noqa except ImportError: import pickle version = pandas.__version__ print("This script generates a storage file for the current arch, system, " "and python version") print(" pandas version: {0}".format(version)) print(" output dir : {0}".format(output_dir)) print(" storage format: pickle") pth = '{0}.pickle'.format(platform_name()) fh = open(os.path.join(output_dir, pth), 'wb') pickle.dump(create_pickle_data(), fh, pickle.HIGHEST_PROTOCOL) fh.close() print("created pickle file: %s" % pth)
Example #4
Source File: generate_legacy_storage_files.py From recruit with Apache License 2.0 | 6 votes |
def write_legacy_pickles(output_dir): # make sure we are < 0.13 compat (in py3) try: from pandas.compat import zip, cPickle as pickle # noqa except ImportError: import pickle version = pandas.__version__ print("This script generates a storage file for the current arch, system, " "and python version") print(" pandas version: {0}".format(version)) print(" output dir : {0}".format(output_dir)) print(" storage format: pickle") pth = '{0}.pickle'.format(platform_name()) fh = open(os.path.join(output_dir, pth), 'wb') pickle.dump(create_pickle_data(), fh, pickle.HIGHEST_PROTOCOL) fh.close() print("created pickle file: %s" % pth)
Example #5
Source File: test_base.py From elasticintel with GNU General Public License v3.0 | 6 votes |
def test_constructor_dtypes_datetime(self): for tz in [None, 'UTC', 'US/Eastern', 'Asia/Tokyo']: idx = pd.date_range('2011-01-01', periods=5, tz=tz) dtype = idx.dtype # pass values without timezone, as DatetimeIndex localizes it for values in [pd.date_range('2011-01-01', periods=5).values, pd.date_range('2011-01-01', periods=5).asi8]: for res in [pd.Index(values, tz=tz), pd.Index(values, dtype=dtype), pd.Index(list(values), tz=tz), pd.Index(list(values), dtype=dtype)]: tm.assert_index_equal(res, idx) # check compat with DatetimeIndex for res in [pd.DatetimeIndex(values, tz=tz), pd.DatetimeIndex(values, dtype=dtype), pd.DatetimeIndex(list(values), tz=tz), pd.DatetimeIndex(list(values), dtype=dtype)]: tm.assert_index_equal(res, idx)
Example #6
Source File: test_pickle.py From Computable with MIT License | 6 votes |
def compare(self, vf): # py3 compat when reading py2 pickle try: data = pandas.read_pickle(vf) except (ValueError) as detail: # trying to read a py3 pickle in py2 return for typ, dv in data.items(): for dt, result in dv.items(): try: expected = self.data[typ][dt] except (KeyError): continue self.compare_element(typ, result, expected)
Example #7
Source File: test_base.py From elasticintel with GNU General Public License v3.0 | 6 votes |
def test_equals_op_multiindex(self): # GH9785 # test comparisons of multiindex from pandas.compat import StringIO df = pd.read_csv(StringIO('a,b,c\n1,2,3\n4,5,6'), index_col=[0, 1]) tm.assert_numpy_array_equal(df.index == df.index, np.array([True, True])) mi1 = MultiIndex.from_tuples([(1, 2), (4, 5)]) tm.assert_numpy_array_equal(df.index == mi1, np.array([True, True])) mi2 = MultiIndex.from_tuples([(1, 2), (4, 6)]) tm.assert_numpy_array_equal(df.index == mi2, np.array([True, False])) mi3 = MultiIndex.from_tuples([(1, 2), (4, 5), (8, 9)]) with tm.assert_raises_regex(ValueError, "Lengths must match"): df.index == mi3 index_a = Index(['foo', 'bar', 'baz']) with tm.assert_raises_regex(ValueError, "Lengths must match"): df.index == index_a tm.assert_numpy_array_equal(index_a == mi3, np.array([False, False, False]))
Example #8
Source File: generate_legacy_storage_files.py From elasticintel with GNU General Public License v3.0 | 6 votes |
def write_legacy_pickles(output_dir): # make sure we are < 0.13 compat (in py3) try: from pandas.compat import zip, cPickle as pickle # noqa except: import pickle version = pandas.__version__ print("This script generates a storage file for the current arch, system, " "and python version") print(" pandas version: {0}".format(version)) print(" output dir : {0}".format(output_dir)) print(" storage format: pickle") pth = '{0}.pickle'.format(platform_name()) fh = open(os.path.join(output_dir, pth), 'wb') pickle.dump(create_pickle_data(), fh, pickle.HIGHEST_PROTOCOL) fh.close() print("created pickle file: %s" % pth)
Example #9
Source File: test_packers.py From vnpy_crypto with MIT License | 6 votes |
def setup_method(self, method): super(TestEncoding, self).setup_method(method) data = { 'A': [compat.u('\u2019')] * 1000, 'B': np.arange(1000, dtype=np.int32), 'C': list(100 * 'abcdefghij'), 'D': date_range(datetime.datetime(2015, 4, 1), periods=1000), 'E': [datetime.timedelta(days=x) for x in range(1000)], 'G': [400] * 1000 } self.frame = { 'float': DataFrame({k: data[k] for k in ['A', 'A']}), 'int': DataFrame({k: data[k] for k in ['B', 'B']}), 'mixed': DataFrame(data), } self.utf_encodings = ['utf8', 'utf16', 'utf32']
Example #10
Source File: test_packers.py From elasticintel with GNU General Public License v3.0 | 6 votes |
def setup_method(self, method): super(TestEncoding, self).setup_method(method) data = { 'A': [compat.u('\u2019')] * 1000, 'B': np.arange(1000, dtype=np.int32), 'C': list(100 * 'abcdefghij'), 'D': date_range(datetime.datetime(2015, 4, 1), periods=1000), 'E': [datetime.timedelta(days=x) for x in range(1000)], 'G': [400] * 1000 } self.frame = { 'float': DataFrame(dict((k, data[k]) for k in ['A', 'A'])), 'int': DataFrame(dict((k, data[k]) for k in ['B', 'B'])), 'mixed': DataFrame(data), } self.utf_encodings = ['utf8', 'utf16', 'utf32']
Example #11
Source File: generate_legacy_storage_files.py From vnpy_crypto with MIT License | 6 votes |
def write_legacy_pickles(output_dir): # make sure we are < 0.13 compat (in py3) try: from pandas.compat import zip, cPickle as pickle # noqa except: import pickle version = pandas.__version__ print("This script generates a storage file for the current arch, system, " "and python version") print(" pandas version: {0}".format(version)) print(" output dir : {0}".format(output_dir)) print(" storage format: pickle") pth = '{0}.pickle'.format(platform_name()) fh = open(os.path.join(output_dir, pth), 'wb') pickle.dump(create_pickle_data(), fh, pickle.HIGHEST_PROTOCOL) fh.close() print("created pickle file: %s" % pth)
Example #12
Source File: test_packers.py From recruit with Apache License 2.0 | 6 votes |
def setup_method(self, method): super(TestEncoding, self).setup_method(method) data = { 'A': [compat.u('\u2019')] * 1000, 'B': np.arange(1000, dtype=np.int32), 'C': list(100 * 'abcdefghij'), 'D': date_range(datetime.datetime(2015, 4, 1), periods=1000), 'E': [datetime.timedelta(days=x) for x in range(1000)], 'G': [400] * 1000 } self.frame = { 'float': DataFrame({k: data[k] for k in ['A', 'A']}), 'int': DataFrame({k: data[k] for k in ['B', 'B']}), 'mixed': DataFrame(data), } self.utf_encodings = ['utf8', 'utf16', 'utf32']
Example #13
Source File: datasource.py From doppelganger with Apache License 2.0 | 6 votes |
def from_csv(infile, dtype=None): """Load marginals from file. Args: infile (unicode): path to csv dtype (dict {unicode -> type}): pandas dtype dict, sets for each column label (dict key) its data type (dict value). Since pandas automatically interprets numbers as numeric types, columns containing codes (e.g. PUMA and state codes) must be set as text types to preserve leading zeros. Returns: Marginals: marginals fetched from a csv file """ if dtype is None: from pandas.compat import text_type dtype = { column: text_type for column in [inputs.PUMA.pums_name, inputs.STATE.pums_name] } data = pandas.read_csv(infile, dtype=dtype) return PumsData(data)
Example #14
Source File: test_pickle.py From elasticintel with GNU General Public License v3.0 | 5 votes |
def decompress_file(self, src_path, dest_path, compression): if compression is None: shutil.copyfile(src_path, dest_path) return if compression == 'gzip': import gzip f = gzip.open(src_path, "r") elif compression == 'bz2': import bz2 f = bz2.BZ2File(src_path, "r") elif compression == 'zip': import zipfile zip_file = zipfile.ZipFile(src_path) zip_names = zip_file.namelist() if len(zip_names) == 1: f = zip_file.open(zip_names.pop()) else: raise ValueError('ZIP file {} error. Only one file per ZIP.' .format(src_path)) elif compression == 'xz': lzma = pandas.compat.import_lzma() f = lzma.LZMAFile(src_path, "r") else: msg = 'Unrecognized compression type: {}'.format(compression) raise ValueError(msg) with open(dest_path, "wb") as fh: fh.write(f.read()) f.close()
Example #15
Source File: test_pickle.py From elasticintel with GNU General Public License v3.0 | 5 votes |
def compare(data, vf, version): # py3 compat when reading py2 pickle try: data = pandas.read_pickle(vf) except (ValueError) as e: if 'unsupported pickle protocol:' in str(e): # trying to read a py3 pickle in py2 return else: raise m = globals() for typ, dv in data.items(): for dt, result in dv.items(): try: expected = data[typ][dt] except (KeyError): if version in ('0.10.1', '0.11.0') and dt == 'reg': break else: raise # use a specific comparator # if available comparator = "compare_{typ}_{dt}".format(typ=typ, dt=dt) comparator = m.get(comparator, m['compare_element']) comparator(result, expected, typ, version) return data
Example #16
Source File: test_pytables.py From Computable with MIT License | 5 votes |
def test_unimplemented_dtypes_table_columns(self): with ensure_clean_store(self.path) as store: l = [('date', datetime.date(2001, 1, 2))] # py3 ok for unicode if not compat.PY3: l.append(('unicode', u('\\u03c3'))) ### currently not supported dtypes #### for n, f in l: df = tm.makeDataFrame() df[n] = f self.assertRaises( TypeError, store.append, 'df1_%s' % n, df) # frame df = tm.makeDataFrame() df['obj1'] = 'foo' df['obj2'] = 'bar' df['datetime1'] = datetime.date(2001, 1, 2) df = df.consolidate().convert_objects() with ensure_clean_store(self.path) as store: # this fails because we have a date in the object block...... self.assertRaises(TypeError, store.append, 'df_unimplemented', df)
Example #17
Source File: test_packers.py From elasticintel with GNU General Public License v3.0 | 5 votes |
def test_string_io(self): df = DataFrame(np.random.randn(10, 2)) s = df.to_msgpack(None) result = read_msgpack(s) tm.assert_frame_equal(result, df) s = df.to_msgpack() result = read_msgpack(s) tm.assert_frame_equal(result, df) s = df.to_msgpack() result = read_msgpack(compat.BytesIO(s)) tm.assert_frame_equal(result, df) s = to_msgpack(None, df) result = read_msgpack(s) tm.assert_frame_equal(result, df) with ensure_clean(self.path) as p: s = df.to_msgpack() fh = open(p, 'wb') fh.write(s) fh.close() result = read_msgpack(p) tm.assert_frame_equal(result, df)
Example #18
Source File: test_base.py From elasticintel with GNU General Public License v3.0 | 5 votes |
def test_constructor_dtypes_timedelta(self): idx = pd.timedelta_range('1 days', periods=5) dtype = idx.dtype for values in [idx.values, idx.asi8]: for res in [pd.Index(values, dtype=dtype), pd.Index(list(values), dtype=dtype)]: tm.assert_index_equal(res, idx) # check compat with TimedeltaIndex for res in [pd.TimedeltaIndex(values, dtype=dtype), pd.TimedeltaIndex(list(values), dtype=dtype)]: tm.assert_index_equal(res, idx)
Example #19
Source File: test_packers.py From elasticintel with GNU General Public License v3.0 | 5 votes |
def test_utf(self): # GH10581 for encoding in self.utf_encodings: for frame in compat.itervalues(self.frame): result = self.encode_decode(frame, encoding=encoding) assert_frame_equal(result, frame)
Example #20
Source File: test_packers.py From elasticintel with GNU General Public License v3.0 | 5 votes |
def test_default_encoding(self): for frame in compat.itervalues(self.frame): result = frame.to_msgpack() expected = frame.to_msgpack(encoding='utf8') assert result == expected result = self.encode_decode(frame) assert_frame_equal(result, frame)
Example #21
Source File: test_packers.py From predictive-maintenance-using-machine-learning with Apache License 2.0 | 5 votes |
def test_default_encoding(self): for frame in compat.itervalues(self.frame): result = frame.to_msgpack() expected = frame.to_msgpack(encoding='utf8') assert result == expected result = self.encode_decode(frame) assert_frame_equal(result, frame)
Example #22
Source File: test_packers.py From predictive-maintenance-using-machine-learning with Apache License 2.0 | 5 votes |
def test_utf(self): # GH10581 for encoding in self.utf_encodings: for frame in compat.itervalues(self.frame): result = self.encode_decode(frame, encoding=encoding) assert_frame_equal(result, frame)
Example #23
Source File: test_packers.py From predictive-maintenance-using-machine-learning with Apache License 2.0 | 5 votes |
def test_string_io(self): df = DataFrame(np.random.randn(10, 2)) s = df.to_msgpack(None) result = read_msgpack(s) tm.assert_frame_equal(result, df) s = df.to_msgpack() result = read_msgpack(s) tm.assert_frame_equal(result, df) s = df.to_msgpack() result = read_msgpack(compat.BytesIO(s)) tm.assert_frame_equal(result, df) s = to_msgpack(None, df) result = read_msgpack(s) tm.assert_frame_equal(result, df) with ensure_clean(self.path) as p: s = df.to_msgpack() with open(p, 'wb') as fh: fh.write(s) result = read_msgpack(p) tm.assert_frame_equal(result, df)
Example #24
Source File: test_packers.py From Computable with MIT License | 5 votes |
def test_string_io(self): df = DataFrame(np.random.randn(10,2)) s = df.to_msgpack(None) result = read_msgpack(s) tm.assert_frame_equal(result,df) s = df.to_msgpack() result = read_msgpack(s) tm.assert_frame_equal(result,df) s = df.to_msgpack() result = read_msgpack(compat.BytesIO(s)) tm.assert_frame_equal(result,df) s = to_msgpack(None,df) result = read_msgpack(s) tm.assert_frame_equal(result, df) with ensure_clean(self.path) as p: s = df.to_msgpack() fh = open(p,'wb') fh.write(s) fh.close() result = read_msgpack(p) tm.assert_frame_equal(result, df)
Example #25
Source File: test_pytables.py From Computable with MIT License | 5 votes |
def compat_assert_produces_warning(w,f): """ don't produce a warning under PY3 """ if compat.PY3: f() else: with tm.assert_produces_warning(expected_warning=w): f()
Example #26
Source File: test_pickle.py From Computable with MIT License | 5 votes |
def _read_pickle(vf, encoding=None, compat=False): from pandas.compat import pickle_compat as pc with open(vf,'rb') as fh: pc.load(fh, encoding=encoding, compat=compat)
Example #27
Source File: generate_legacy_pickles.py From Computable with MIT License | 5 votes |
def write_legacy_pickles(): # force our cwd to be the first searched import sys sys.path.insert(0,'.') import os, os.path import numpy as np import pandas import pandas.util.testing as tm import platform as pl # make sure we are < 0.13 compat (in py3) try: from pandas.compat import zip, cPickle as pickle except: import pickle sys_version = version = pandas.__version__ if len(sys.argv) < 2: exit("{0} <version> <output_dir>".format(sys.argv[0])) version = str(sys.argv[1]) output_dir = str(sys.argv[2]) print("This script generates a pickle file for the current arch, system, and python version") print(" system version: {0}".format(sys_version)) print(" output version: {0}".format(version)) print(" output dir : {0}".format(output_dir)) # construct a reasonable platform name f = '_'.join([ str(version), str(pl.machine()), str(pl.system().lower()), str(pl.python_version()) ]) pth = '{0}.pickle'.format(f) fh = open(os.path.join(output_dir,pth),'wb') pickle.dump(create_data(),fh,pickle.HIGHEST_PROTOCOL) fh.close() print("created pickle file: %s" % pth)
Example #28
Source File: pickle_compat.py From Computable with MIT License | 5 votes |
def load(fh, encoding=None, compat=False, is_verbose=False): """load a pickle, with a provided encoding if compat is True: fake the old class hierarchy if it works, then return the new type objects Parameters ---------- fh: a filelike object encoding: an optional encoding compat: provide Series compatibility mode, boolean, default False is_verbose: show exception output """ try: if compat: pandas.core.series.Series = DeprecatedSeries pandas.core.series.TimeSeries = DeprecatedTimeSeries pandas.sparse.series.SparseSeries = DeprecatedSparseSeries pandas.sparse.series.SparseTimeSeries = DeprecatedSparseTimeSeries fh.seek(0) if encoding is not None: up = Unpickler(fh, encoding=encoding) else: up = Unpickler(fh) up.is_verbose = is_verbose return up.load() except: raise finally: if compat: pandas.core.series.Series = Series pandas.core.series.Series = TimeSeries pandas.sparse.series.SparseSeries = SparseSeries pandas.sparse.series.SparseTimeSeries = SparseTimeSeries
Example #29
Source File: test_packers.py From vnpy_crypto with MIT License | 5 votes |
def test_default_encoding(self): for frame in compat.itervalues(self.frame): result = frame.to_msgpack() expected = frame.to_msgpack(encoding='utf8') assert result == expected result = self.encode_decode(frame) assert_frame_equal(result, frame)
Example #30
Source File: test_packers.py From vnpy_crypto with MIT License | 5 votes |
def test_string_io(self): df = DataFrame(np.random.randn(10, 2)) s = df.to_msgpack(None) result = read_msgpack(s) tm.assert_frame_equal(result, df) s = df.to_msgpack() result = read_msgpack(s) tm.assert_frame_equal(result, df) s = df.to_msgpack() result = read_msgpack(compat.BytesIO(s)) tm.assert_frame_equal(result, df) s = to_msgpack(None, df) result = read_msgpack(s) tm.assert_frame_equal(result, df) with ensure_clean(self.path) as p: s = df.to_msgpack() fh = open(p, 'wb') fh.write(s) fh.close() result = read_msgpack(p) tm.assert_frame_equal(result, df)