예제 #1
0
    def test_format_timedelta_ticks_wide(self):
        if is_platform_mac():
            pytest.skip("skip on mac for precision display issue on older mpl")

        expected_labels = [
            '00:00:00',
            '1 days 03:46:40',
            '2 days 07:33:20',
            '3 days 11:20:00',
            '4 days 15:06:40',
            '5 days 18:53:20',
            '6 days 22:40:00',
            '8 days 02:26:40',
            ''
        ]

        rng = timedelta_range('0', periods=10, freq='1 d')
        df = DataFrame(np.random.randn(len(rng), 3), rng)
        ax = df.plot(fontsize=2)
        fig = ax.get_figure()
        fig.canvas.draw()
        labels = ax.get_xticklabels()
        self.assertEqual(len(labels), len(expected_labels))
        for l, l_expected in zip(labels, expected_labels):
            self.assertEqual(l.get_text(), l_expected)
예제 #2
0
    def test_format_timedelta_ticks_wide(self):
        if is_platform_mac():
            pytest.skip("skip on mac for precision display issue on older mpl")

        expected_labels = [
            '00:00:00',
            '1 days 03:46:40',
            '2 days 07:33:20',
            '3 days 11:20:00',
            '4 days 15:06:40',
            '5 days 18:53:20',
            '6 days 22:40:00',
            '8 days 02:26:40',
            ''
        ]

        rng = timedelta_range('0', periods=10, freq='1 d')
        df = DataFrame(np.random.randn(len(rng), 3), rng)
        ax = df.plot(fontsize=2)
        fig = ax.get_figure()
        fig.canvas.draw()
        labels = ax.get_xticklabels()
        self.assertEqual(len(labels), len(expected_labels))
        for l, l_expected in zip(labels, expected_labels):
            self.assertEqual(l.get_text(), l_expected)
예제 #3
0
def s3_base(worker_id):
    """
    Fixture for mocking S3 interaction.

    Sets up moto server in separate process locally
    Return url for motoserver/moto CI service
    """
    pytest.importorskip("s3fs")
    pytest.importorskip("boto3")

    with tm.ensure_safe_environment_variables():
        # temporary workaround as moto fails for botocore >= 1.11 otherwise,
        # see https://github.com/spulec/moto/issues/1924 & 1952
        os.environ.setdefault("AWS_ACCESS_KEY_ID", "foobar_key")
        os.environ.setdefault("AWS_SECRET_ACCESS_KEY", "foobar_secret")
        if os.environ.get("PANDAS_CI", "0") == "1":
            if is_platform_arm() or is_platform_mac() or is_platform_windows():
                # NOT RUN on Windows/MacOS/ARM, only Ubuntu
                # - subprocess in CI can cause timeouts
                # - Azure pipelines/Github Actions do not support
                #   container services for the above OSs
                # - CircleCI will probably hit the Docker rate pull limit
                pytest.skip("S3 tests do not have a corresponding service in "
                            "Windows, MacOS or ARM platforms")
            else:
                yield "http://localhost:5000"
        else:
            requests = pytest.importorskip("requests")
            pytest.importorskip("moto", minversion="1.3.14")
            pytest.importorskip("flask")  # server mode needs flask too

            # Launching moto in server mode, i.e., as a separate process
            # with an S3 endpoint on localhost

            worker_id = "5" if worker_id == "master" else worker_id.lstrip(
                "gw")
            endpoint_port = f"555{worker_id}"
            endpoint_uri = f"http://127.0.0.1:{endpoint_port}/"

            # pipe to null to avoid logging in terminal
            with subprocess.Popen(
                    shlex.split(f"moto_server s3 -p {endpoint_port}"),
                    stdout=subprocess.DEVNULL,
                    stderr=subprocess.DEVNULL,
            ) as proc:

                timeout = 5
                while timeout > 0:
                    try:
                        # OK to go once server is accepting connections
                        r = requests.get(endpoint_uri)
                        if r.ok:
                            break
                    except Exception:
                        pass
                    timeout -= 0.1
                    time.sleep(0.1)
                yield endpoint_uri

                proc.terminate()
예제 #4
0
    def test_format_timedelta_ticks_narrow(self):
        if is_platform_mac():
            pytest.skip("skip on mac for precision display issue on older mpl")

        expected_labels = [
            '00:00:00.00000000{:d}'.format(i) for i in range(10)
        ]

        rng = timedelta_range('0', periods=10, freq='ns')
        df = DataFrame(np.random.randn(len(rng), 3), rng)
        ax = df.plot(fontsize=2)
        fig = ax.get_figure()
        fig.canvas.draw()
        labels = ax.get_xticklabels()
        self.assertEqual(len(labels), len(expected_labels))
        for l, l_expected in zip(labels, expected_labels):
            self.assertEqual(l.get_text(), l_expected)
예제 #5
0
    def test_format_timedelta_ticks_narrow(self):
        if is_platform_mac():
            pytest.skip("skip on mac for precision display issue on older mpl")

        expected_labels = [
            '00:00:00.00000000{:d}'.format(i)
            for i in range(10)]

        rng = timedelta_range('0', periods=10, freq='ns')
        df = DataFrame(np.random.randn(len(rng), 3), rng)
        ax = df.plot(fontsize=2)
        fig = ax.get_figure()
        fig.canvas.draw()
        labels = ax.get_xticklabels()
        self.assertEqual(len(labels), len(expected_labels))
        for l, l_expected in zip(labels, expected_labels):
            self.assertEqual(l.get_text(), l_expected)
예제 #6
0
def test_close_file_handle_on_invalid_usecols(all_parsers):
    # GH 45384
    parser = all_parsers

    error = ValueError
    if parser.engine == "pyarrow":
        pyarrow = pytest.importorskip("pyarrow")
        error = pyarrow.lib.ArrowKeyError
        if is_ci_environment() and (is_platform_windows() or is_platform_mac()):
            # GH#45547 causes timeouts on windows/mac builds
            pytest.skip("GH#45547 causing timeouts on windows/mac builds 2022-01-22")

    with tm.ensure_clean("test.csv") as fname:
        Path(fname).write_text("col1,col2\na,b\n1,2")
        with tm.assert_produces_warning(False):
            with pytest.raises(error, match="col3"):
                parser.read_csv(fname, usecols=["col1", "col2", "col3"])
        # unlink fails on windows if file handles still point to it
        os.unlink(fname)
예제 #7
0
        assert isinstance(get_engine('auto'), PyArrowImpl)
        assert isinstance(get_engine('pyarrow'), PyArrowImpl)
        assert isinstance(get_engine('fastparquet'), FastParquetImpl)

    with pd.option_context('io.parquet.engine', 'fastparquet'):
        assert isinstance(get_engine('auto'), FastParquetImpl)
        assert isinstance(get_engine('pyarrow'), PyArrowImpl)
        assert isinstance(get_engine('fastparquet'), FastParquetImpl)

    with pd.option_context('io.parquet.engine', 'auto'):
        assert isinstance(get_engine('auto'), PyArrowImpl)
        assert isinstance(get_engine('pyarrow'), PyArrowImpl)
        assert isinstance(get_engine('fastparquet'), FastParquetImpl)


@pytest.mark.xfail(is_platform_windows() or is_platform_mac(),
                   reason="reading pa metadata failing on Windows/mac",
                   strict=True)
def test_cross_engine_pa_fp(df_cross_compat, pa, fp):
    # cross-compat with differing reading/writing engines

    df = df_cross_compat
    with tm.ensure_clean() as path:
        df.to_parquet(path, engine=pa, compression=None)

        result = read_parquet(path, engine=fp)
        tm.assert_frame_equal(result, df)

        result = read_parquet(path, engine=fp, columns=['a', 'd'])
        tm.assert_frame_equal(result, df[['a', 'd']])
예제 #8
0
        assert isinstance(get_engine('auto'), PyArrowImpl)
        assert isinstance(get_engine('pyarrow'), PyArrowImpl)
        assert isinstance(get_engine('fastparquet'), FastParquetImpl)

    with pd.option_context('io.parquet.engine', 'fastparquet'):
        assert isinstance(get_engine('auto'), FastParquetImpl)
        assert isinstance(get_engine('pyarrow'), PyArrowImpl)
        assert isinstance(get_engine('fastparquet'), FastParquetImpl)

    with pd.option_context('io.parquet.engine', 'auto'):
        assert isinstance(get_engine('auto'), PyArrowImpl)
        assert isinstance(get_engine('pyarrow'), PyArrowImpl)
        assert isinstance(get_engine('fastparquet'), FastParquetImpl)


@pytest.mark.xfail(is_platform_windows() or is_platform_mac(),
                   reason="reading pa metadata failing on Windows/mac")
def test_cross_engine_pa_fp(df_cross_compat, pa, fp):
    # cross-compat with differing reading/writing engines

    df = df_cross_compat
    with tm.ensure_clean() as path:
        df.to_parquet(path, engine=pa, compression=None)

        result = read_parquet(path, engine=fp)
        tm.assert_frame_equal(result, df)

        result = read_parquet(path, engine=fp, columns=['a', 'd'])
        tm.assert_frame_equal(result, df[['a', 'd']])

예제 #9
0
from pandas.compat import (
    is_ci_environment,
    is_platform_mac,
    is_platform_windows,
)
import pandas.util._test_decorators as td

from pandas import (
    DataFrame,
    Series,
)
import pandas._testing as tm

# TODO(GH#44584): Mark these as pytest.mark.single_cpu
pytestmark = pytest.mark.skipif(
    is_ci_environment() and (is_platform_windows() or is_platform_mac()),
    reason="On GHA CI, Windows can fail with "
    "'Windows fatal exception: stack overflow' "
    "and MacOS can timeout",
)


@td.skip_if_no("numba")
@pytest.mark.filterwarnings("ignore:\n")
class TestEWM:
    def test_invalid_update(self):
        df = DataFrame({"a": range(5), "b": range(5)})
        online_ewm = df.head(2).ewm(0.5).online()
        with pytest.raises(
                ValueError,
                match=
    result = getattr(ds.rolling(window, closed="left"), func)()
    expected = Series(values, index=index)
    tm.assert_series_equal(result, expected)


def test_rolling_sem(frame_or_series):
    # GH: 26476
    obj = frame_or_series([0, 1, 2])
    result = obj.rolling(2, min_periods=1).sem()
    if isinstance(result, DataFrame):
        result = Series(result[0].values)
    expected = Series([np.nan] + [0.7071067811865476] * 2)
    tm.assert_series_equal(result, expected)


@pytest.mark.xfail(is_platform_arm() and not is_platform_mac(),
                   reason="GH 38921")
@pytest.mark.parametrize(
    ("func", "third_value", "values"),
    [
        ("var", 1, [5e33, 0, 0.5, 0.5, 2, 0]),
        ("std", 1, [7.071068e16, 0, 0.7071068, 0.7071068, 1.414214, 0]),
        ("var", 2, [5e33, 0.5, 0, 0.5, 2, 0]),
        ("std", 2, [7.071068e16, 0.7071068, 0, 0.7071068, 1.414214, 0]),
    ],
)
def test_rolling_var_numerical_issues(func, third_value, values):
    # GH: 37051
    ds = Series([99999999999999999, 1, third_value, 2, 3, 1, 1])
    result = getattr(ds.rolling(2), func)()
    expected = Series([np.nan] + values)
예제 #11
0
class TestUInt64Index(NumericInt):

    _index_cls = UInt64Index

    @pytest.fixture
    def dtype(self):
        return np.uint64

    @pytest.fixture(
        params=["int64", "float64", "category", "datetime64", "object"], )
    def invalid_dtype(self, request):
        return request.param

    @pytest.fixture
    def simple_index(self, dtype):
        # compat with shared Int64/Float64 tests
        return self._index_cls(np.arange(5, dtype=dtype))

    @pytest.fixture(
        params=[
            [2**63, 2**63 + 10, 2**63 + 15, 2**63 + 20, 2**63 + 25],
            [2**63 + 25, 2**63 + 20, 2**63 + 15, 2**63 + 10, 2**63],
        ],
        ids=["index_inc", "index_dec"],
    )
    def index(self, request):
        return self._index_cls(request.param)

    def test_constructor(self, dtype):
        index_cls = self._index_cls

        idx = index_cls([1, 2, 3])
        res = Index([1, 2, 3], dtype=dtype)
        tm.assert_index_equal(res, idx)

        idx = index_cls([1, 2**63])
        res = Index([1, 2**63], dtype=dtype)
        tm.assert_index_equal(res, idx)

        idx = index_cls([1, 2**63])
        res = Index([1, 2**63])
        tm.assert_index_equal(res, idx)

        idx = Index([-1, 2**63], dtype=object)
        res = Index(np.array([-1, 2**63], dtype=object))
        tm.assert_index_equal(res, idx)

        # https://github.com/pandas-dev/pandas/issues/29526
        idx = index_cls([1, 2**63 + 1], dtype=dtype)
        res = Index([1, 2**63 + 1], dtype=dtype)
        tm.assert_index_equal(res, idx)

    @pytest.mark.xfail(
        not (is_platform_arm() and is_platform_mac()),
        reason="https://github.com/numpy/numpy/issues/19146",
    )
    def test_constructor_does_not_cast_to_float(self):
        # https://github.com/numpy/numpy/issues/19146
        values = [0, np.iinfo(np.uint64).max]

        result = UInt64Index(values)
        assert list(result) == values
예제 #12
0
    ]

    with tm.ensure_clean(ext) as path:
        writer = _OpenpyxlWriter(path)
        writer.write_cells(initial_cells, sheet_name=sheet_name)
        writer.write_cells(merge_cells, sheet_name=sheet_name)

        wks = writer.sheets[sheet_name]
        xcell_b1 = wks["B1"]
        xcell_a2 = wks["A2"]
        assert xcell_b1.font == openpyxl_sty_merged
        assert xcell_a2.font == openpyxl_sty_merged


@pytest.mark.xfail(
    openpyxl_gt301 and PY37 and is_platform_mac(), reason="broken change in openpyxl"
)
@pytest.mark.parametrize(
    "mode,expected", [("w", ["baz"]), ("a", ["foo", "bar", "baz"])]
)
def test_write_append_mode(ext, mode, expected):
    df = DataFrame([1], columns=["baz"])

    with tm.ensure_clean(ext) as f:
        wb = openpyxl.Workbook()
        wb.worksheets[0].title = "foo"
        wb.worksheets[0]["A1"].value = "foo"
        wb.create_sheet("bar")
        wb.worksheets[1]["A1"].value = "bar"
        wb.save(f)
예제 #13
0
    result = getattr(ds.rolling(window, closed="left"), func)()
    expected = Series(values, index=index)
    tm.assert_series_equal(result, expected)


def test_rolling_sem(frame_or_series):
    # GH: 26476
    obj = frame_or_series([0, 1, 2])
    result = obj.rolling(2, min_periods=1).sem()
    if isinstance(result, DataFrame):
        result = Series(result[0].values)
    expected = Series([np.nan] + [0.7071067811865476] * 2)
    tm.assert_series_equal(result, expected)


@pytest.mark.xfail(is_platform_arm() and not is_platform_mac(), reason="GH 38921")
@pytest.mark.parametrize(
    ("func", "third_value", "values"),
    [
        ("var", 1, [5e33, 0, 0.5, 0.5, 2, 0]),
        ("std", 1, [7.071068e16, 0, 0.7071068, 0.7071068, 1.414214, 0]),
        ("var", 2, [5e33, 0.5, 0, 0.5, 2, 0]),
        ("std", 2, [7.071068e16, 0.7071068, 0, 0.7071068, 1.414214, 0]),
    ],
)
def test_rolling_var_numerical_issues(func, third_value, values):
    # GH: 37051
    ds = Series([99999999999999999, 1, third_value, 2, 3, 1, 1])
    result = getattr(ds.rolling(2), func)()
    expected = Series([np.nan] + values)
    tm.assert_series_equal(result, expected)