analog_daddy.csv_io
¶
UnfuckCSV
¶
Bases: BaseModel
Abstraction for working with non-standard CSV files.
Subclass this to provide your downstream code a unified representation of a CSV and its associated metadata.
This abstraction restricts the horrors of reading/writing a fucked
up CSV to this abyss of Tartarus. These motherfuckers pretend to
follow the CSV standard and are usually observed in the corporate
wilderness. It welcomes you with the warmth of the ",", and also
makes you feel at home with the correct escaping of quotes. But,
your fate was already sealed when you decided to utilize it as your
data source. You, a novice, try to read it into a dataframe and once
inside the dataframe, you notice that your index column is of type
str. "But it contained all numbers!" you protest. Turns out that
same index column contained rows at regular intervals which are
essentially nested columns.
You try to escape and you run into your knight in shining armour
which is the most beautiful and tidiest data of the wide format you
have seen. Surely, this would be amenable to processing in a
vectorized fashion, you think optimistically. But when you try
reading it into a dataframe, your spine tingles and your brow
sweats, when you see that the header was the second row and suddenly
all your columns have been cast to a str type. That's when you
know that there is no escape from the drudgery of the malformed CSV.
Your ancestors faced it and now you face it. You wish for someone to
have tamed this beast and scour the depths of GitHub and Stack
Overflow to obtain the tranquilizer to put this beast to sleep.
Unfortunately, you don't find one. Make your sacrifice count and use
this so nothing else in your codebase suffers as badly as this part
does.
Source code in src/analog_daddy/csv_io.py
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 | |
contents
instance-attribute
¶
Actual contents of the CSV file.
format_type = None
class-attribute
instance-attribute
¶
Format type of the CSV file, for classification. Add more formats as needed.
header_lineno = 1
class-attribute
instance-attribute
¶
Line number of the header in the file.
n_rows = None
class-attribute
instance-attribute
¶
Optional upper bound on number of rows to read from the CSV.
classify()
¶
Classify CSV file with a quick peek of the contents.
Logic to classify the CSV file into one of the
:attr:format_type. Usually, this can be done without reading
the file as a dataframe and rather by checking for patterns.
Source code in src/analog_daddy/csv_io.py
df_detail_tidy(index_col='Point', var_name='Output', var_value='Nominal')
¶
An example to tidy a Dataframe containing nested rows.
Source code in src/analog_daddy/csv_io.py
read_csv(null_values=('nil', 'eval error', 'eval err', 'canceled', 'pending', '-', ''))
¶
Read the classified CSV file into a polars DataFrame.
Logic to read the classified file into the dataframe. It might not guarantee that the dataframe is tidy, since doing that might require some dataframe level processing.
Source code in src/analog_daddy/csv_io.py
tidy_df()
¶
Logic to tidy up the dataframe.