Python pandas.testing() Examples

The following are 21 code examples of pandas.testing(). 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_pytorch_model_export.py    From mlflow with Apache License 2.0 6 votes vote down vote up
def test_sagemaker_docker_model_scoring_with_sequential_model_and_default_conda_env(
        model, model_path, data, sequential_predicted):
    mlflow.pytorch.save_model(pytorch_model=model, path=model_path, conda_env=None)

    scoring_response = score_model_in_sagemaker_docker_container(
            model_uri=model_path,
            data=data[0],
            content_type=pyfunc_scoring_server.CONTENT_TYPE_JSON_SPLIT_ORIENTED,
            flavor=mlflow.pyfunc.FLAVOR_NAME,
            activity_polling_timeout_seconds=360)
    deployed_model_preds = pd.DataFrame(json.loads(scoring_response.content))

    np.testing.assert_array_almost_equal(
        deployed_model_preds.values[:, 0],
        sequential_predicted,
        decimal=4) 
Example #2
Source File: test_timestamp.py    From pandas-gbq with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_localize_df_with_timestamp_column(module_under_test):
    df = pandas.DataFrame(
        {
            "integer_col": [1, 2, 3],
            "timestamp_col": pandas.Series(
                [
                    "2011-01-01 01:02:03",
                    "2012-02-02 04:05:06",
                    "2013-03-03 07:08:09",
                ],
                dtype="datetime64[ns]",
            ),
            "float_col": [0.1, 0.2, 0.3],
        }
    )
    expected = df.copy()
    expected["timestamp_col"] = df["timestamp_col"].dt.tz_localize("UTC")
    bq_schema = [
        {"name": "integer_col", "type": "INTEGER"},
        {"name": "timestamp_col", "type": "TIMESTAMP"},
        {"name": "float_col", "type": "FLOAT"},
    ]

    localized = module_under_test.localize_df(df, bq_schema)
    pandas.testing.assert_frame_equal(localized, expected) 
Example #3
Source File: test_pytorch_model_export.py    From mlflow with Apache License 2.0 6 votes vote down vote up
def test_pyfunc_model_serving_with_main_scoped_subclassed_model_and_custom_pickle_module(
        main_scoped_subclassed_model, model_path, data):
    mlflow.pytorch.save_model(
        path=model_path,
        pytorch_model=main_scoped_subclassed_model,
        conda_env=None,
        pickle_module=mlflow_pytorch_pickle_module)

    scoring_response = pyfunc_serve_and_score_model(
            model_uri=model_path,
            data=data[0],
            content_type=pyfunc_scoring_server.CONTENT_TYPE_JSON_SPLIT_ORIENTED,
            extra_args=["--no-conda"])
    assert scoring_response.status_code == 200

    deployed_model_preds = pd.DataFrame(json.loads(scoring_response.content))
    np.testing.assert_array_almost_equal(
        deployed_model_preds.values[:, 0],
        _predict(model=main_scoped_subclassed_model, data=data),
        decimal=4) 
Example #4
Source File: test_pytorch_model_export.py    From mlflow with Apache License 2.0 6 votes vote down vote up
def test_pyfunc_model_serving_with_module_scoped_subclassed_model_and_default_conda_env(
        module_scoped_subclassed_model, model_path, data):
    mlflow.pytorch.save_model(
        path=model_path,
        pytorch_model=module_scoped_subclassed_model,
        conda_env=None,
        code_paths=[__file__])

    scoring_response = pyfunc_serve_and_score_model(
            model_uri=model_path,
            data=data[0],
            content_type=pyfunc_scoring_server.CONTENT_TYPE_JSON_SPLIT_ORIENTED,
            extra_args=["--no-conda"])
    assert scoring_response.status_code == 200

    deployed_model_preds = pd.DataFrame(json.loads(scoring_response.content))
    np.testing.assert_array_almost_equal(
        deployed_model_preds.values[:, 0],
        _predict(model=module_scoped_subclassed_model, data=data),
        decimal=4) 
Example #5
Source File: test__pandas_helpers.py    From python-bigquery with Apache License 2.0 6 votes vote down vote up
def test_get_column_or_index_with_multiindex(module_under_test):
    dataframe = pandas.DataFrame(
        {"column_name": [1, 2, 3, 4, 5, 6]},
        index=pandas.MultiIndex.from_tuples(
            [("a", 0), ("a", 1), ("b", 0), ("b", 1), ("c", 0), ("c", 1)],
            names=["letters", "numbers"],
        ),
    )

    series = module_under_test.get_column_or_index(dataframe, "letters")
    expected = pandas.Series(["a", "a", "b", "b", "c", "c"], name="letters")
    pandas.testing.assert_series_equal(series, expected)

    series = module_under_test.get_column_or_index(dataframe, "numbers")
    expected = pandas.Series([0, 1, 0, 1, 0, 1], name="numbers")
    pandas.testing.assert_series_equal(series, expected) 
Example #6
Source File: test_pytorch_model_export.py    From mlflow with Apache License 2.0 6 votes vote down vote up
def test_log_model(sequential_model, data, sequential_predicted):
    old_uri = tracking.get_tracking_uri()
    # should_start_run tests whether or not calling log_model() automatically starts a run.
    for should_start_run in [False, True]:
        with TempDir(chdr=True, remove_on_exit=True) as tmp:
            try:
                tracking.set_tracking_uri(tmp.path("test"))
                if should_start_run:
                    mlflow.start_run()

                artifact_path = "pytorch"
                mlflow.pytorch.log_model(sequential_model, artifact_path=artifact_path)
                model_uri = "runs:/{run_id}/{artifact_path}".format(
                    run_id=mlflow.active_run().info.run_id,
                    artifact_path=artifact_path)

                # Load model
                sequential_model_loaded = mlflow.pytorch.load_model(model_uri=model_uri)

                test_predictions = _predict(sequential_model_loaded, data)
                np.testing.assert_array_equal(test_predictions, sequential_predicted)
            finally:
                mlflow.end_run()
                tracking.set_tracking_uri(old_uri) 
Example #7
Source File: test_api.py    From twitter-stock-recommendation with MIT License 5 votes vote down vote up
def test_testing(self):

        from pandas import testing
        self.check(testing, self.funcs) 
Example #8
Source File: test_api.py    From recruit with Apache License 2.0 5 votes vote down vote up
def test_testing(self):

        from pandas import testing
        self.check(testing, self.funcs) 
Example #9
Source File: test_pytorch_model_export.py    From mlflow with Apache License 2.0 5 votes vote down vote up
def test_load_model_succeeds_when_data_is_model_file_instead_of_directory(
        module_scoped_subclassed_model, model_path, data):
    """
    This test verifies that PyTorch models saved in older versions of MLflow are loaded successfully
    by ``mlflow.pytorch.load_model``. The ``data`` path associated with these older models is
    serialized PyTorch model file, as opposed to the current format: a directory containing a
    serialized model file and pickle module information.
    """
    artifact_path = "pytorch_model"
    with mlflow.start_run():
        mlflow.pytorch.log_model(
            artifact_path=artifact_path,
            pytorch_model=module_scoped_subclassed_model,
            conda_env=None)
        model_path = _download_artifact_from_uri("runs:/{run_id}/{artifact_path}".format(
            run_id=mlflow.active_run().info.run_id, artifact_path=artifact_path))

    model_conf_path = os.path.join(model_path, "MLmodel")
    model_conf = Model.load(model_conf_path)
    pyfunc_conf = model_conf.flavors.get(pyfunc.FLAVOR_NAME)
    assert pyfunc_conf is not None
    model_data_path = os.path.join(model_path, pyfunc_conf[pyfunc.DATA])
    assert os.path.exists(model_data_path)
    assert mlflow.pytorch._SERIALIZED_TORCH_MODEL_FILE_NAME in os.listdir(model_data_path)
    pyfunc_conf[pyfunc.DATA] = os.path.join(
        model_data_path, mlflow.pytorch._SERIALIZED_TORCH_MODEL_FILE_NAME)
    model_conf.save(model_conf_path)

    loaded_pyfunc = pyfunc.load_pyfunc(model_path)

    np.testing.assert_array_almost_equal(
        loaded_pyfunc.predict(data[0]),
        pd.DataFrame(_predict(model=module_scoped_subclassed_model, data=data)),
        decimal=4) 
Example #10
Source File: test_pytorch_model_export.py    From mlflow with Apache License 2.0 5 votes vote down vote up
def test_load_pyfunc_succeeds_when_data_is_model_file_instead_of_directory(
        module_scoped_subclassed_model, model_path, data):
    """
    This test verifies that PyTorch models saved in older versions of MLflow are loaded successfully
    by ``mlflow.pytorch.load_model``. The ``data`` path associated with these older models is
    serialized PyTorch model file, as opposed to the current format: a directory containing a
    serialized model file and pickle module information.
    """
    mlflow.pytorch.save_model(
        path=model_path,
        pytorch_model=module_scoped_subclassed_model,
        conda_env=None)

    model_conf_path = os.path.join(model_path, "MLmodel")
    model_conf = Model.load(model_conf_path)
    pyfunc_conf = model_conf.flavors.get(pyfunc.FLAVOR_NAME)
    assert pyfunc_conf is not None
    model_data_path = os.path.join(model_path, pyfunc_conf[pyfunc.DATA])
    assert os.path.exists(model_data_path)
    assert mlflow.pytorch._SERIALIZED_TORCH_MODEL_FILE_NAME in os.listdir(model_data_path)
    pyfunc_conf[pyfunc.DATA] = os.path.join(
        model_data_path, mlflow.pytorch._SERIALIZED_TORCH_MODEL_FILE_NAME)
    model_conf.save(model_conf_path)

    loaded_pyfunc = pyfunc.load_pyfunc(model_path)

    np.testing.assert_array_almost_equal(
        loaded_pyfunc.predict(data[0]),
        pd.DataFrame(_predict(model=module_scoped_subclassed_model, data=data)),
        decimal=4) 
Example #11
Source File: test_pytorch_model_export.py    From mlflow with Apache License 2.0 5 votes vote down vote up
def test_load_model_from_remote_uri_succeeds(
        sequential_model, model_path, mock_s3_bucket, data, sequential_predicted):
    mlflow.pytorch.save_model(sequential_model, model_path)

    artifact_root = "s3://{bucket_name}".format(bucket_name=mock_s3_bucket)
    artifact_path = "model"
    artifact_repo = S3ArtifactRepository(artifact_root)
    artifact_repo.log_artifacts(model_path, artifact_path=artifact_path)

    model_uri = artifact_root + "/" + artifact_path
    sequential_model_loaded = mlflow.pytorch.load_model(model_uri=model_uri)
    np.testing.assert_array_equal(_predict(sequential_model_loaded, data), sequential_predicted) 
Example #12
Source File: test_pytorch_model_export.py    From mlflow with Apache License 2.0 5 votes vote down vote up
def test_save_and_load_model(sequential_model, model_path, data, sequential_predicted):
    mlflow.pytorch.save_model(sequential_model, model_path)

    # Loading pytorch model
    sequential_model_loaded = mlflow.pytorch.load_model(model_path)
    np.testing.assert_array_equal(_predict(sequential_model_loaded, data), sequential_predicted)

    # Loading pyfunc model
    pyfunc_loaded = mlflow.pyfunc.load_pyfunc(model_path)
    np.testing.assert_array_almost_equal(
        pyfunc_loaded.predict(data[0]).values[:, 0], sequential_predicted, decimal=4) 
Example #13
Source File: test_api.py    From elasticintel with GNU General Public License v3.0 5 votes vote down vote up
def test_testing(self):

        from pandas import testing
        self.check(testing, self.funcs) 
Example #14
Source File: test__pandas_helpers.py    From python-bigquery with Apache License 2.0 5 votes vote down vote up
def test_get_column_or_index_with_datetimeindex(module_under_test):
    datetimes = [
        datetime.datetime(2000, 1, 2, 3, 4, 5, 101),
        datetime.datetime(2006, 7, 8, 9, 10, 11, 202),
        datetime.datetime(2012, 1, 14, 15, 16, 17, 303),
    ]
    dataframe = pandas.DataFrame(
        {"column_name": [1, 2, 3]},
        index=pandas.DatetimeIndex(datetimes, name="index_name"),
    )
    series = module_under_test.get_column_or_index(dataframe, "index_name")
    expected = pandas.Series(datetimes, name="index_name")
    pandas.testing.assert_series_equal(series, expected) 
Example #15
Source File: test__pandas_helpers.py    From python-bigquery with Apache License 2.0 5 votes vote down vote up
def test_get_column_or_index_with_named_index(module_under_test):
    dataframe = pandas.DataFrame(
        {"column_name": [1, 2, 3]}, index=pandas.Index([4, 5, 6], name="index_name")
    )
    series = module_under_test.get_column_or_index(dataframe, "index_name")
    expected = pandas.Series([4, 5, 6], name="index_name")
    pandas.testing.assert_series_equal(series, expected) 
Example #16
Source File: test__pandas_helpers.py    From python-bigquery with Apache License 2.0 5 votes vote down vote up
def test_get_column_or_index_with_column(module_under_test):
    dataframe = pandas.DataFrame({"column_name": [1, 2, 3], "other_column": [4, 5, 6]})
    series = module_under_test.get_column_or_index(dataframe, "column_name")
    expected = pandas.Series([1, 2, 3], name="column_name")
    pandas.testing.assert_series_equal(series, expected) 
Example #17
Source File: test__pandas_helpers.py    From python-bigquery with Apache License 2.0 5 votes vote down vote up
def test_get_column_or_index_with_both_prefers_column(module_under_test):
    dataframe = pandas.DataFrame(
        {"some_name": [1, 2, 3]}, index=pandas.Index([0, 1, 2], name="some_name")
    )
    series = module_under_test.get_column_or_index(dataframe, "some_name")
    expected = pandas.Series([1, 2, 3], name="some_name")
    pandas.testing.assert_series_equal(series, expected) 
Example #18
Source File: test_timestamp.py    From pandas-gbq with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_localize_df_with_no_timestamp_columns(module_under_test):
    df = pandas.DataFrame(
        {"integer_col": [1, 2, 3], "float_col": [0.1, 0.2, 0.3]}
    )
    original = df.copy()
    bq_schema = [
        {"name": "integer_col", "type": "INTEGER"},
        {"name": "float_col", "type": "FLOAT"},
    ]

    localized = module_under_test.localize_df(df, bq_schema)

    # DataFrames with no TIMESTAMP columns should be unchanged.
    assert localized is df
    pandas.testing.assert_frame_equal(localized, original) 
Example #19
Source File: test_timestamp.py    From pandas-gbq with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_localize_df_with_empty_dataframe(module_under_test):
    df = pandas.DataFrame({"timestamp_col": [], "other_col": []})
    original = df.copy()
    bq_schema = [
        {"name": "timestamp_col", "type": "TIMESTAMP"},
        {"name": "other_col", "type": "STRING"},
    ]

    localized = module_under_test.localize_df(df, bq_schema)

    # Empty DataFrames should be unchanged.
    assert localized is df
    pandas.testing.assert_frame_equal(localized, original) 
Example #20
Source File: test_api.py    From predictive-maintenance-using-machine-learning with Apache License 2.0 5 votes vote down vote up
def test_testing(self):

        from pandas import testing
        self.check(testing, self.funcs) 
Example #21
Source File: test_api.py    From vnpy_crypto with MIT License 5 votes vote down vote up
def test_testing(self):

        from pandas import testing
        self.check(testing, self.funcs)