@@ -66,6 +66,25 @@ def test_none_passthrough(self):
6666 result = _normalize_scalar (None )
6767 self .assertIsNone (result )
6868
69+ def test_timestamp_with_timezone (self ):
70+ """Timezone-aware pd.Timestamp is converted to ISO 8601 with tz offset."""
71+ ts = pd .Timestamp ("2024-06-15 10:30:00" , tz = "UTC" )
72+ result = _normalize_scalar (ts )
73+ self .assertIn ("2024-06-15T10:30:00" , result )
74+ self .assertIsInstance (result , str )
75+
76+ def test_numpy_int32 (self ):
77+ """np.int32 is also converted to Python int."""
78+ result = _normalize_scalar (np .int32 (7 ))
79+ self .assertIsInstance (result , int )
80+ self .assertEqual (result , 7 )
81+
82+ def test_numpy_float32 (self ):
83+ """np.float32 is also converted to Python float."""
84+ result = _normalize_scalar (np .float32 (2.5 ))
85+ self .assertIsInstance (result , float )
86+ self .assertAlmostEqual (result , 2.5 , places = 5 )
87+
6988
7089class TestDataframeToRecords (unittest .TestCase ):
7190 """Unit tests for dataframe_to_records()."""
@@ -165,6 +184,40 @@ def test_mixed_types(self):
165184 self .assertEqual (rec ["createdon" ], "2024-06-01T00:00:00" )
166185 self .assertNotIn ("notes" , rec )
167186
187+ def test_timezone_aware_timestamp (self ):
188+ """Timezone-aware Timestamp in DataFrame is converted to ISO string with tz."""
189+ ts = pd .Timestamp ("2024-06-15 10:30:00" , tz = "US/Eastern" )
190+ df = pd .DataFrame ([{"createdon" : ts }])
191+ result = dataframe_to_records (df )
192+ self .assertIn ("2024-06-15T10:30:00" , result [0 ]["createdon" ])
193+ self .assertIsInstance (result [0 ]["createdon" ], str )
194+
195+ def test_multiple_rows_some_with_nan (self ):
196+ """Multi-row DataFrame with mixed NaN positions drops correct keys per row."""
197+ df = pd .DataFrame (
198+ [
199+ {"name" : "A" , "phone" : "555-0100" , "city" : None },
200+ {"name" : "B" , "phone" : None , "city" : "Seattle" },
201+ {"name" : None , "phone" : "555-0300" , "city" : "Portland" },
202+ ]
203+ )
204+ result = dataframe_to_records (df )
205+ self .assertEqual (result [0 ], {"name" : "A" , "phone" : "555-0100" })
206+ self .assertEqual (result [1 ], {"name" : "B" , "city" : "Seattle" })
207+ self .assertEqual (result [2 ], {"phone" : "555-0300" , "city" : "Portland" })
208+
209+ def test_multiple_rows_na_as_null (self ):
210+ """Multi-row DataFrame with na_as_null=True includes None for all missing values."""
211+ df = pd .DataFrame (
212+ [
213+ {"name" : "A" , "phone" : None },
214+ {"name" : None , "phone" : "555-0200" },
215+ ]
216+ )
217+ result = dataframe_to_records (df , na_as_null = True )
218+ self .assertEqual (result [0 ], {"name" : "A" , "phone" : None })
219+ self .assertEqual (result [1 ], {"name" : None , "phone" : "555-0200" })
220+
168221
169222if __name__ == "__main__" :
170223 unittest .main ()
0 commit comments