Ejemplo n.º 1
0
 def test_empty_with_columns(self):
     """ An empty dataframe should still have the datatypes cast. """
     # get columns and dtypes
     columns = ["time", "space"]
     dtypes = {"time": "ops_datetime", "space": float}
     # create 2 dfs, one empty one with values
     df_empty = pd.DataFrame(columns=columns)
     df_full = pd.DataFrame([[1, 1.2]], columns=columns)
     # run cast_dtypes on both and compare
     out_empty = upd.cast_dtypes(df_empty, dtype=dtypes).dtypes
     out_full = upd.cast_dtypes(df_full, dtype=dtypes).dtypes
     assert (out_empty == out_full).all()
Ejemplo n.º 2
0
 def test_empty_no_columns(self):
     """ Ensure empty dataframes with no columns just returns. """
     df = pd.DataFrame()
     out = upd.cast_dtypes(df, dtype={"bob": int})
     assert isinstance(out, pd.DataFrame)
     assert out.empty
     assert len(out.columns) == 0
Ejemplo n.º 3
0
def _df_to_waveform_bulk(df):
    """Ensure the dataframe has appropriate columns and return."""
    current_columns = set(df.columns)
    required_columns = list(WAVEFORM_REQUEST_DTYPES)
    # ensure columns exist
    if not current_columns.issuperset(required_columns):
        missing = set(required_columns) - set(current_columns)
        msg = (f"Dataframe is missing the following columns to be valid input"
               f" for bulk waveform request {missing}")
        raise ValidationError(msg)
    out = df[list(required_columns)]
    return cast_dtypes(out, dtype=WAVEFORM_REQUEST_DTYPES)[required_columns]
Ejemplo n.º 4
0
 def test_time_delta(self, time_df):
     """ Test that timedelta dtype. """
     out1 = upd.cast_dtypes(time_df, {"delta": "ops_timedelta"})["delta"]
     out2 = to_timedelta64(time_df["delta"])
     assert (out1 == out2).all()
Ejemplo n.º 5
0
 def test_time_dtype(self, time_df):
     """ Test time dtype. """
     out1 = upd.cast_dtypes(time_df, {"time": "ops_datetime"})["time"]
     out2 = to_datetime64(time_df["time"])
     assert (out1 == out2).all()
Ejemplo n.º 6
0
 def test_inplace(self, simple_df):
     """Ensure the changes can be made inplace."""
     out = upd.cast_dtypes(simple_df, {"latitude": int}, inplace=True)
     assert out is simple_df
     out2 = upd.cast_dtypes(simple_df, {"longitude": int})
     assert out2 is not simple_df
Ejemplo n.º 7
0
 def test_basic(self, simple_df):
     """ simple test for casting datatypes. """
     out = upd.cast_dtypes(simple_df, {"time": str})
     assert all([isinstance(x, str) for x in out["time"]])
Ejemplo n.º 8
0
 def test_utc_datetime(self, time_df):
     """ Tests for converting to UTCDateTime. """
     out = upd.cast_dtypes(time_df, {"time": "utcdatetime"})
     assert all([isinstance(x, obspy.UTCDateTime) for x in out["time"]])