@@ -2,7 +2,7 @@ import path from "path";
22import chai , { assert , expect } from "chai" ;
33import { describe , it } from "mocha" ;
44import chaiAsPromised from "chai-as-promised" ;
5- import { DataFrame , readCSV , Series , streamCSV , toCSV } from "../../dist/danfojs-node/src" ;
5+ import { DataFrame , readCSV , Series , streamCSV , toCSV , toJSON } from "../../dist/danfojs-node/src" ;
66import fs from 'fs' ;
77import process from 'process' ;
88
@@ -112,6 +112,59 @@ describe("readCSV", function () {
112112 const filePath = path . join ( testSamplesDir , "invalid.csv" ) ;
113113 await expect ( readCSV ( filePath ) ) . to . be . rejectedWith ( "ENOENT: no such file or directory" ) ;
114114 } ) ;
115+
116+ it ( "Preserves leading zeros when dtype is string" , async function ( ) {
117+ const filePath = path . join ( testSamplesDir , "leading_zeros.csv" ) ;
118+ // Create test CSV file
119+ fs . writeFileSync ( filePath , "codes\n012345\n001234" ) ;
120+
121+ try {
122+ const df = await readCSV ( filePath , {
123+ frameConfig : {
124+ dtypes : [ "string" ]
125+ }
126+ } ) ;
127+
128+ assert . deepEqual ( df . values , [ [ "012345" ] , [ "001234" ] ] ) ;
129+ assert . deepEqual ( df . dtypes , [ "string" ] ) ;
130+
131+ // Verify the values are actually strings
132+ const jsonData = toJSON ( df ) ;
133+ assert . deepEqual ( jsonData , [ { codes : "012345" } , { codes : "001234" } ] ) ;
134+
135+ // Clean up
136+ fs . unlinkSync ( filePath ) ;
137+ } catch ( error ) {
138+ // Clean up even if test fails
139+ fs . unlinkSync ( filePath ) ;
140+ throw error ;
141+ }
142+ } ) ;
143+
144+ it ( "Converts to numbers when dtype is not string" , async function ( ) {
145+ const filePath = path . join ( testSamplesDir , "leading_zeros.csv" ) ;
146+ // Create test CSV file
147+ fs . writeFileSync ( filePath , "codes\n012345\n001234" ) ;
148+
149+ try {
150+ const df = await readCSV ( filePath ) ; // default behavior without string dtype
151+
152+ // Values should be converted to numbers
153+ assert . deepEqual ( df . values , [ [ 12345 ] , [ 1234 ] ] ) ;
154+ assert . deepEqual ( df . dtypes , [ "int32" ] ) ;
155+
156+ // Verify JSON output
157+ const jsonData = toJSON ( df ) ;
158+ assert . deepEqual ( jsonData , [ { codes : 12345 } , { codes : 1234 } ] ) ;
159+
160+ // Clean up
161+ fs . unlinkSync ( filePath ) ;
162+ } catch ( error ) {
163+ // Clean up even if test fails
164+ fs . unlinkSync ( filePath ) ;
165+ throw error ;
166+ }
167+ } ) ;
115168} ) ;
116169
117170describe ( "streamCSV" , function ( ) {
0 commit comments