當前位置: 首頁>>代碼示例>>Python>>正文


Python common._get_handle方法代碼示例

本文整理匯總了Python中pandas.io.common._get_handle方法的典型用法代碼示例。如果您正苦於以下問題:Python common._get_handle方法的具體用法?Python common._get_handle怎麽用?Python common._get_handle使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在pandas.io.common的用法示例。


在下文中一共展示了common._get_handle方法的7個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: test_compression_size_fh

# 需要導入模塊: from pandas.io import common [as 別名]
# 或者: from pandas.io.common import _get_handle [as 別名]
def test_compression_size_fh(obj, method, compression_only):
    with tm.ensure_clean() as path:
        f, handles = icom._get_handle(path, 'w', compression=compression_only)
        with catch_to_csv_depr():
            with f:
                getattr(obj, method)(f)
                assert not f.closed
            assert f.closed
            compressed_size = os.path.getsize(path)
    with tm.ensure_clean() as path:
        f, handles = icom._get_handle(path, 'w', compression=None)
        with catch_to_csv_depr():
            with f:
                getattr(obj, method)(f)
                assert not f.closed
        assert f.closed
        uncompressed_size = os.path.getsize(path)
        assert uncompressed_size > compressed_size 
開發者ID:Frank-qlu,項目名稱:recruit,代碼行數:20,代碼來源:test_compression.py

示例2: test_compression_warning

# 需要導入模塊: from pandas.io import common [as 別名]
# 或者: from pandas.io.common import _get_handle [as 別名]
def test_compression_warning(compression_only):
    # Assert that passing a file object to to_csv while explicitly specifying a
    # compression protocol triggers a RuntimeWarning, as per GH21227.
    # Note that pytest has an issue that causes assert_produces_warning to fail
    # in Python 2 if the warning has occurred in previous tests
    # (see https://git.io/fNEBm & https://git.io/fNEBC). Hence, should this
    # test fail in just Python 2 builds, it likely indicates that other tests
    # are producing RuntimeWarnings, thereby triggering the pytest bug.
    df = pd.DataFrame(100 * [[0.123456, 0.234567, 0.567567],
                             [12.32112, 123123.2, 321321.2]],
                      columns=['X', 'Y', 'Z'])
    with tm.ensure_clean() as path:
        f, handles = icom._get_handle(path, 'w', compression=compression_only)
        with tm.assert_produces_warning(RuntimeWarning,
                                        check_stacklevel=False):
            with f:
                df.to_csv(f, compression=compression_only) 
開發者ID:Frank-qlu,項目名稱:recruit,代碼行數:19,代碼來源:test_compression.py

示例3: test_compression_size_fh

# 需要導入模塊: from pandas.io import common [as 別名]
# 或者: from pandas.io.common import _get_handle [as 別名]
def test_compression_size_fh(obj, method, compression_only):

    with tm.ensure_clean() as filename:
        f, _handles = _get_handle(filename, 'w', compression=compression_only)
        with f:
            getattr(obj, method)(f)
            assert not f.closed
        assert f.closed
        compressed = os.path.getsize(filename)
    with tm.ensure_clean() as filename:
        f, _handles = _get_handle(filename, 'w', compression=None)
        with f:
            getattr(obj, method)(f)
            assert not f.closed
        assert f.closed
        uncompressed = os.path.getsize(filename)
        assert uncompressed > compressed


# GH 21227 
開發者ID:birforce,項目名稱:vnpy_crypto,代碼行數:22,代碼來源:test_common.py

示例4: test_to_csv_compression

# 需要導入模塊: from pandas.io import common [as 別名]
# 或者: from pandas.io.common import _get_handle [as 別名]
def test_to_csv_compression(self, s, encoding, compression):

        with ensure_clean() as filename:

            s.to_csv(filename, compression=compression, encoding=encoding,
                     header=True)
            # test the round trip - to_csv -> read_csv
            result = pd.read_csv(filename, compression=compression,
                                 encoding=encoding, index_col=0, squeeze=True)
            assert_series_equal(s, result)

            # test the round trip using file handle - to_csv -> read_csv
            f, _handles = _get_handle(filename, 'w', compression=compression,
                                      encoding=encoding)
            with f:
                s.to_csv(f, encoding=encoding, header=True)
            result = pd.read_csv(filename, compression=compression,
                                 encoding=encoding, index_col=0, squeeze=True)
            assert_series_equal(s, result)

            # explicitly ensure file was compressed
            with tm.decompress_file(filename, compression) as fh:
                text = fh.read().decode(encoding or 'utf8')
                assert s.name in text

            with tm.decompress_file(filename, compression) as fh:
                assert_series_equal(s, pd.read_csv(fh,
                                                   index_col=0,
                                                   squeeze=True,
                                                   encoding=encoding)) 
開發者ID:Frank-qlu,項目名稱:recruit,代碼行數:32,代碼來源:test_io.py

示例5: test_to_csv_compression

# 需要導入模塊: from pandas.io import common [as 別名]
# 或者: from pandas.io.common import _get_handle [as 別名]
def test_to_csv_compression(self, df, encoding, compression):

        with ensure_clean() as filename:

            df.to_csv(filename, compression=compression, encoding=encoding)
            # test the round trip - to_csv -> read_csv
            result = read_csv(filename, compression=compression,
                              index_col=0, encoding=encoding)
            assert_frame_equal(df, result)

            # test the round trip using file handle - to_csv -> read_csv
            f, _handles = _get_handle(filename, 'w', compression=compression,
                                      encoding=encoding)
            with f:
                df.to_csv(f, encoding=encoding)
            result = pd.read_csv(filename, compression=compression,
                                 encoding=encoding, index_col=0, squeeze=True)
            assert_frame_equal(df, result)

            # explicitly make sure file is compressed
            with tm.decompress_file(filename, compression) as fh:
                text = fh.read().decode(encoding or 'utf8')
                for col in df.columns:
                    assert col in text

            with tm.decompress_file(filename, compression) as fh:
                assert_frame_equal(df, read_csv(fh,
                                                index_col=0,
                                                encoding=encoding)) 
開發者ID:Frank-qlu,項目名稱:recruit,代碼行數:31,代碼來源:test_to_csv.py

示例6: test_compression_warning

# 需要導入模塊: from pandas.io import common [as 別名]
# 或者: from pandas.io.common import _get_handle [as 別名]
def test_compression_warning(compression_only):
    df = DataFrame(100 * [[0.123456, 0.234567, 0.567567],
                          [12.32112, 123123.2, 321321.2]],
                   columns=['X', 'Y', 'Z'])
    with tm.ensure_clean() as filename:
        f, _handles = _get_handle(filename, 'w', compression=compression_only)
        with tm.assert_produces_warning(RuntimeWarning,
                                        check_stacklevel=False):
            with f:
                df.to_csv(f, compression=compression_only) 
開發者ID:birforce,項目名稱:vnpy_crypto,代碼行數:12,代碼來源:test_common.py

示例7: to_pickle

# 需要導入模塊: from pandas.io import common [as 別名]
# 或者: from pandas.io.common import _get_handle [as 別名]
def to_pickle(obj, path, compression='infer', protocol=pkl.HIGHEST_PROTOCOL):
    """
    Pickle (serialize) object to input file path

    Parameters
    ----------
    obj : any object
    path : string
        File path
    compression : {'infer', 'gzip', 'bz2', 'xz', None}, default 'infer'
        a string representing the compression to use in the output file

        .. versionadded:: 0.20.0
    protocol : int
        Int which indicates which protocol should be used by the pickler,
        default HIGHEST_PROTOCOL (see [1], paragraph 12.1.2). The possible
        values for this parameter depend on the version of Python. For Python
        2.x, possible values are 0, 1, 2. For Python>=3.0, 3 is a valid value.
        For Python >= 3.4, 4 is a valid value. A negative value for the
        protocol parameter is equivalent to setting its value to
        HIGHEST_PROTOCOL.

        .. [1] https://docs.python.org/3/library/pickle.html
        .. versionadded:: 0.21.0


    """
    path = _stringify_path(path)
    inferred_compression = _infer_compression(path, compression)
    f, fh = _get_handle(path, 'wb',
                        compression=inferred_compression,
                        is_text=False)
    if protocol < 0:
        protocol = pkl.HIGHEST_PROTOCOL
    try:
        pkl.dump(obj, f, protocol=protocol)
    finally:
        for _f in fh:
            _f.close() 
開發者ID:nccgroup,項目名稱:Splunking-Crime,代碼行數:41,代碼來源:pickle.py


注:本文中的pandas.io.common._get_handle方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。