Skip to content

Commit

Permalink
Correctly export all NumPy datetime64 and timedelta64 resolutions. (#65)
Browse files Browse the repository at this point in the history
  • Loading branch information
bbassett-tibco committed May 30, 2024
1 parent 65ca640 commit 2c8e0fd
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
4 changes: 2 additions & 2 deletions spotfire/sbdf.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1584,9 +1584,9 @@ cdef int _export_infer_valuetype_from_pandas_dtype(series, series_description):
return sbdf_c.SBDF_DOUBLETYPEID
elif dtype == "Float64":
return sbdf_c.SBDF_DOUBLETYPEID
elif dtype.startswith("datetime64[ns"):
elif dtype.startswith("datetime64["):
return sbdf_c.SBDF_DATETIMETYPEID
elif dtype == "timedelta64[ns]":
elif dtype.startswith("timedelta64["):
return sbdf_c.SBDF_TIMESPANTYPEID
elif dtype == "string":
return sbdf_c.SBDF_STRINGTYPEID
Expand Down
35 changes: 35 additions & 0 deletions spotfire/test/test_sbdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import pandas as pd
import pandas.testing as pdtest
import numpy as np
import geopandas as gpd
import matplotlib.pyplot
import seaborn
Expand Down Expand Up @@ -428,6 +429,40 @@ def test_tz_aware_datetime(self):
self.assertAlmostEqual(val, now, msg=f"df2[{col}] = {repr(val)}",
delta=datetime.timedelta(milliseconds=1)) # SBDF has millisecond resolution

def test_numpy_datetime_resolution(self):
"""Verify that different NumPy resolutions for datetime64 dtypes export properly."""
target = datetime.datetime(2020, 1, 1)
inputs = {
's': 1577836800,
'ms': 1577836800000,
'us': 1577836800000000,
'ns': 1577836800000000000,
}
for resolution, timestamp in inputs.items():
with self.subTest(resolution=resolution):
array = np.array([[0], [timestamp]]).astype(f"datetime64[{resolution}]")
dataframe = pd.DataFrame(array, columns=["x"])
df2 = self._roundtrip_dataframe(dataframe)
val = df2.at[1, 'x']
self.assertEqual(val, target)

def test_numpy_timedelta_resolution(self):
"""Verify that different NumPy resolutions for timedelta64 dtypes export properly."""
target = datetime.timedelta(seconds=38400)
inputs = {
's': 38400,
'ms': 38400000,
'us': 38400000000,
'ns': 38400000000000,
}
for resolution, timestamp in inputs.items():
with self.subTest(resolution=resolution):
array = np.array([[0], [timestamp]]).astype(f"timedelta64[{resolution}]")
dataframe = pd.DataFrame(array, columns=["x"])
df2 = self._roundtrip_dataframe(dataframe)
val = df2.at[1, 'x']
self.assertEqual(val, target)

def test_image_matplot(self):
"""Verify Matplotlib figures export properly."""
matplotlib.pyplot.clf()
Expand Down

0 comments on commit 2c8e0fd

Please sign in to comment.