Skip to content

Commit 621202e

Browse files
committed
Datetime type fixes
1 parent 877aaf8 commit 621202e

File tree

2 files changed

+23
-13
lines changed

2 files changed

+23
-13
lines changed

src/gmn_python_api/trajectory_summary_reader.py

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
import os.path
77
from io import StringIO
88
from typing import Any
9+
from typing import Optional
910

10-
import numpy as np
1111
import numpy.typing as npt
1212
import pandas as pd # type: ignore
1313
from pandas._typing import FilePathOrBuffer # type: ignore
@@ -20,8 +20,9 @@
2020

2121
def read_trajectory_summary_as_dataframe(
2222
filepath_or_buffer: FilePathOrBuffer,
23-
camel_case_column_names: bool = False,
24-
avro_compatible: bool = False,
23+
camel_case_column_names: Optional[bool] = False,
24+
avro_compatible: Optional[bool] = False,
25+
avro_long_beginning_utc_time: Optional[bool] = True,
2526
) -> pd.DataFrame:
2627
"""
2728
Reads a trajectory summary file into a Pandas DataFrame.
@@ -30,6 +31,9 @@ def read_trajectory_summary_as_dataframe(
3031
:param camel_case_column_names: If True, column names will be camel cased e.g. m_deg
3132
:param avro_compatible: If True, the rows in the dataframe will match the avsc
3233
schema with row.to_dict().
34+
:param avro_long_beginning_utc_time: If True, the time column will be converted from
35+
a datetime object to an int64 epoch time which is compatible with the long
36+
timestamp-micros avro type.
3337
3438
:return: Pandas DataFrame of the trajectory summary file.
3539
"""
@@ -78,12 +82,16 @@ def read_trajectory_summary_as_dataframe(
7882

7983
if avro_compatible:
8084
camel_case_column_names = True
81-
trajectory_df["Beginning (UTC Time)"] = trajectory_df[
82-
"Beginning (UTC Time)"
83-
].astype(np.int64) / int(1e6)
84-
trajectory_df["Beginning (UTC Time)"] = trajectory_df[
85-
"Beginning (UTC Time)"
86-
].astype(np.int64)
85+
86+
if avro_long_beginning_utc_time:
87+
# convert datetime nano to micro epoch and round to int
88+
trajectory_df["Beginning (UTC Time)"] = (
89+
trajectory_df["Beginning (UTC Time)"].astype("int64") / 1e3
90+
)
91+
trajectory_df["Beginning (UTC Time)"] = (
92+
trajectory_df["Beginning (UTC Time)"].round(0).astype("int64")
93+
)
94+
8795
trajectory_df["IAU (code)"] = trajectory_df["IAU (code)"].astype("unicode")
8896
trajectory_df["Schema (version)"] = trajectory_df["Schema (version)"].astype(
8997
"unicode"

src/gmn_python_api/trajectory_summary_schema.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616

1717
SCHEMA_VERSION = "2.0"
18-
""""""
18+
"""The supported trajectory summary data format version."""
1919

2020
_MODEL_TRAJECTORY_SUMMARY_FILE_PATH = os.path.join(
2121
os.path.dirname(__file__),
@@ -25,12 +25,12 @@
2525
"""Model v2.0 trajectory summary file."""
2626

2727
_AVRO_PATH = os.path.join(tempfile.gettempdir(), "trajectory_summary.avro")
28-
""""""
28+
"""The path for the temporary Avro file used to create the .avsc schema"""
2929

3030
_AVSC_PATH = os.path.join(
3131
tempfile.gettempdir(), f"trajectory_summary_schema_{SCHEMA_VERSION}.avsc"
3232
)
33-
""""""
33+
"""The path for the temporary Avro schema file that will be returned as a string."""
3434

3535

3636
def get_trajectory_summary_avro_schema() -> Dict[str, Dict[str, Any]]:
@@ -39,7 +39,9 @@ def get_trajectory_summary_avro_schema() -> Dict[str, Dict[str, Any]]:
3939
:return: The Avro schema in .avsc format.
4040
"""
4141
data_frame = gmn_python_api.read_trajectory_summary_as_dataframe( # type: ignore
42-
_MODEL_TRAJECTORY_SUMMARY_FILE_PATH, avro_compatible=True
42+
_MODEL_TRAJECTORY_SUMMARY_FILE_PATH,
43+
avro_compatible=True,
44+
avro_long_beginning_utc_time=False,
4345
)
4446

4547
pdx.to_avro(_AVRO_PATH, data_frame)

0 commit comments

Comments
 (0)