Skip to content

Commit

Permalink
ENH: remove is_datetime64tz_type (#522)
Browse files Browse the repository at this point in the history
  • Loading branch information
bifbof authored Sep 14, 2023
1 parent d835098 commit 101baa3
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 32 deletions.
16 changes: 8 additions & 8 deletions tests/io/test_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,15 @@ def test_set_datatime_tz(self):
# check if tz is added to the datatime column
file = os.path.join("tests", "data", "positionfixes.csv")
pfs = ti.read_positionfixes_csv(file, sep=";", index_col="id")
assert pd.api.types.is_datetime64tz_dtype(pfs["tracked_at"])
assert isinstance(pfs["tracked_at"].dtype, pd.DatetimeTZDtype)

# check if a timezone will be set without storing the timezone
date_format = "%Y-%m-%d %H:%M:%S"
tmp_file = os.path.join("tests", "data", "positionfixes_test_2.csv")
pfs.as_positionfixes.to_csv(tmp_file, sep=";", date_format=date_format)
pfs = ti.read_positionfixes_csv(tmp_file, sep=";", index_col="id", tz="utc")

assert pd.api.types.is_datetime64tz_dtype(pfs["tracked_at"])
assert isinstance(pfs["tracked_at"].dtype, pd.DatetimeTZDtype)

# check if a warning is raised if 'tz' is not provided
with pytest.warns(UserWarning):
Expand Down Expand Up @@ -115,15 +115,15 @@ def test_set_datatime_tz(self):
# check if tz is added to the datatime column
file = os.path.join("tests", "data", "triplegs.csv")
tpls = ti.read_triplegs_csv(file, sep=";", index_col="id")
assert pd.api.types.is_datetime64tz_dtype(tpls["started_at"])
assert isinstance(tpls["started_at"].dtype, pd.DatetimeTZDtype)

# check if a timezone will be set without storing the timezone
tmp_file = os.path.join("tests", "data", "triplegs_test_2.csv")
date_format = "%Y-%m-%d %H:%M:%S"
tpls.as_triplegs.to_csv(tmp_file, sep=";", date_format=date_format)
tpls = ti.read_triplegs_csv(tmp_file, sep=";", index_col="id", tz="utc")

assert pd.api.types.is_datetime64tz_dtype(tpls["started_at"])
assert isinstance(tpls["started_at"].dtype, pd.DatetimeTZDtype)

# check if a warning is raised if 'tz' is not provided
with pytest.warns(UserWarning):
Expand Down Expand Up @@ -180,15 +180,15 @@ def test_set_datatime_tz(self):
# check if tz is added to the datatime column
file = os.path.join("tests", "data", "staypoints.csv")
sp = ti.read_staypoints_csv(file, sep=";", index_col="id")
assert pd.api.types.is_datetime64tz_dtype(sp["started_at"])
assert isinstance(sp["started_at"].dtype, pd.DatetimeTZDtype)

# check if a timezone will be without storing the timezone
tmp_file = os.path.join("tests", "data", "staypoints_test_2.csv")
date_format = "%Y-%m-%d %H:%M:%S"
sp.as_staypoints.to_csv(tmp_file, sep=";", date_format=date_format)
sp = ti.read_staypoints_csv(tmp_file, sep=";", index_col="id", tz="utc")

assert pd.api.types.is_datetime64tz_dtype(sp["started_at"])
assert isinstance(sp["started_at"].dtype, pd.DatetimeTZDtype)

# check if a warning is raised if 'tz' is not provided
with pytest.warns(UserWarning):
Expand Down Expand Up @@ -305,15 +305,15 @@ def test_set_datatime_tz(self):
# check if tz is added to the datatime column
file = os.path.join("tests", "data", "trips.csv")
trips = ti.read_trips_csv(file, sep=";", index_col="id")
assert pd.api.types.is_datetime64tz_dtype(trips["started_at"])
assert isinstance(trips["started_at"].dtype, pd.DatetimeTZDtype)

# check if a timezone will be set without storing the timezone
tmp_file = os.path.join("tests", "data", "trips_test_2.csv")
date_format = "%Y-%m-%d %H:%M:%S"
trips.as_trips.to_csv(tmp_file, sep=";", date_format=date_format)
trips = ti.read_trips_csv(tmp_file, sep=";", index_col="id", tz="utc")

assert pd.api.types.is_datetime64tz_dtype(trips["started_at"])
assert isinstance(trips["started_at"].dtype, pd.DatetimeTZDtype)

# check if a warning is raised if 'tz' is not provided
with pytest.warns(UserWarning):
Expand Down
2 changes: 1 addition & 1 deletion trackintel/io/from_geopandas.py
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ def _trackintel_model(gdf, set_names=None, geom_col=None, crs=None, tz_cols=None

if tz_cols is not None:
for col in tz_cols:
if not pd.api.types.is_datetime64tz_dtype(gdf[col]):
if not isinstance(gdf[col].dtype, pd.DatetimeTZDtype):
try:
gdf[col] = _localize_timestamp(dt_series=gdf[col], pytz_tzinfo=tz, col_name=col)
except ValueError:
Expand Down
6 changes: 3 additions & 3 deletions trackintel/model/positionfixes.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ def _validate(obj, validate_geometry=True):
f" {_required_columns}, but it has [{', '.join(obj.columns)}]."
)
# check timestamp dtypes
assert pd.api.types.is_datetime64tz_dtype(
obj["tracked_at"]
assert isinstance(
obj["tracked_at"].dtype, pd.DatetimeTZDtype
), f"dtype of tracked_at is {obj['tracked_at'].dtype} but has to be datetime64 and timezone aware"

# check geometry
Expand All @@ -86,7 +86,7 @@ def _check(obj, validate_geometry=True):
return False
if obj.shape[0] <= 0:
return False
if not pd.api.types.is_datetime64tz_dtype(obj["tracked_at"]):
if not isinstance(obj["tracked_at"].dtype, pd.DatetimeTZDtype):
return False
if validate_geometry:
return obj.geometry.is_valid.all() and obj.geometry.iloc[0].geom_type == "Point"
Expand Down
12 changes: 6 additions & 6 deletions trackintel/model/staypoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,11 @@ def _validate(obj, validate_geometry=True):
f" {_required_columns}, but it has {', '.join(obj.columns)}."
)
# check timestamp dtypes
assert pd.api.types.is_datetime64tz_dtype(
obj["started_at"]
assert isinstance(
obj["started_at"].dtype, pd.DatetimeTZDtype
), f"dtype of started_at is {obj['started_at'].dtype} but has to be tz aware datetime64"
assert pd.api.types.is_datetime64tz_dtype(
obj["finished_at"]
assert isinstance(
obj["finished_at"].dtype, pd.DatetimeTZDtype
), f"dtype of finished_at is {obj['finished_at'].dtype} but has to be tz aware datetime64"

if validate_geometry:
Expand All @@ -86,9 +86,9 @@ def _check(obj, validate_geometry=True):
"""Check does the same as _validate but returns bool instead of potentially raising an error."""
if any([c not in obj.columns for c in _required_columns]):
return False
if not pd.api.types.is_datetime64tz_dtype(obj["started_at"]):
if not isinstance(obj["started_at"].dtype, pd.DatetimeTZDtype):
return False
if not pd.api.types.is_datetime64tz_dtype(obj["finished_at"]):
if not isinstance(obj["finished_at"].dtype, pd.DatetimeTZDtype):
return False
if validate_geometry:
return obj.geometry.is_valid.all() and obj.geometry.iloc[0].geom_type == "Point"
Expand Down
8 changes: 4 additions & 4 deletions trackintel/model/tours.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ def _validate(obj):
)

# check timestamp dtypes
assert pd.api.types.is_datetime64tz_dtype(
obj["started_at"]
assert isinstance(
obj["started_at"].dtype, pd.DatetimeTZDtype
), f"dtype of started_at is {obj['started_at'].dtype} but has to be datetime64 and timezone aware"
assert pd.api.types.is_datetime64tz_dtype(
obj["finished_at"]
assert isinstance(
obj["finished_at"].dtype, pd.DatetimeTZDtype
), f"dtype of finished_at is {obj['finished_at'].dtype} but has to be datetime64 and timezone aware"

def to_csv(self, filename, *args, **kwargs):
Expand Down
12 changes: 6 additions & 6 deletions trackintel/model/triplegs.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,11 @@ def _validate(obj, validate_geometry=True):
)

# check timestamp dtypes
assert pd.api.types.is_datetime64tz_dtype(
obj["started_at"]
assert isinstance(
obj["started_at"].dtype, pd.DatetimeTZDtype
), f"dtype of started_at is {obj['started_at'].dtype} but has to be datetime64 and timezone aware"
assert pd.api.types.is_datetime64tz_dtype(
obj["finished_at"]
assert isinstance(
obj["finished_at"].dtype, pd.DatetimeTZDtype
), f"dtype of finished_at is {obj['finished_at'].dtype} but has to be datetime64 and timezone aware"

# check geometry
Expand All @@ -89,9 +89,9 @@ def _check(obj, validate_geometry=True):
return False
if obj.shape[0] <= 0:
return False
if not pd.api.types.is_datetime64tz_dtype(obj["started_at"]):
if not isinstance(obj["started_at"].dtype, pd.DatetimeTZDtype):
return False
if not pd.api.types.is_datetime64tz_dtype(obj["finished_at"]):
if not isinstance(obj["finished_at"].dtype, pd.DatetimeTZDtype):
return False
if validate_geometry:
return obj.geometry.is_valid.all() and obj.geometry.iloc[0].geom_type == "LineString"
Expand Down
8 changes: 4 additions & 4 deletions trackintel/model/trips.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,11 @@ def _validate(obj):
)

# check timestamp dtypes
assert pd.api.types.is_datetime64tz_dtype(
obj["started_at"]
assert isinstance(
obj["started_at"].dtype, pd.DatetimeTZDtype
), f"dtype of started_at is {obj['started_at'].dtype} but has to be datetime64 and timezone aware"
assert pd.api.types.is_datetime64tz_dtype(
obj["finished_at"]
assert isinstance(
obj["finished_at"].dtype, pd.DatetimeTZDtype
), f"dtype of finished_at is {obj['finished_at'].dtype} but has to be datetime64 and timezone aware"

# Check geometry if Trips is a GeoDataFrame
Expand Down

0 comments on commit 101baa3

Please sign in to comment.