@@ -60,13 +60,13 @@ def csv_bytes_list_to_numpy(
6060 """Efficiently converts a list of bytes representing whose concatenation
6161 represents a CSV file into a numpy array. Includes header specifies
6262 whether the bytes contains an initial header line."""
63+ if not csv_bytes_list :
64+ return np .empty ((0 ,))
65+ num_cols = csv_bytes_list [0 ].count (b"," ) + 1
6366 try :
6467 import polars as pl
6568
6669 try :
67- if not csv_bytes_list :
68- raise ValueError ("No data found to parse" )
69- num_cols = csv_bytes_list [0 ].count (b"," ) + 1
7070 out : npt .NDArray [np .float64 ] = (
7171 pl .read_csv (
7272 io .BytesIO (b"" .join (csv_bytes_list )),
@@ -77,10 +77,8 @@ def csv_bytes_list_to_numpy(
7777 .to_numpy ()
7878 .astype (np .float64 )
7979 )
80- if out .shape [0 ] == 0 :
81- raise ValueError ("No data found to parse" )
82- except pl .exceptions .NoDataError as exc :
83- raise ValueError ("No data found to parse" ) from exc
80+ except pl .exceptions .NoDataError :
81+ return np .empty ((0 , num_cols ))
8482 except ImportError :
8583 with warnings .catch_warnings ():
8684 warnings .filterwarnings ("ignore" )
@@ -91,10 +89,11 @@ def csv_bytes_list_to_numpy(
9189 dtype = np .float64 ,
9290 ndmin = 1 ,
9391 )
94- if out .shape == (0 ,):
95- raise ValueError ("No data found to parse" ) # pylint: disable=W0707
9692 if len (out .shape ) == 1 :
97- out = out .reshape (1 , - 1 )
93+ if out .shape [0 ] == 0 : # No data read
94+ out = np .empty ((0 , num_cols ))
95+ else :
96+ out = out .reshape (1 , - 1 )
9897
9998 return out
10099
0 commit comments