Code Reference
df_file_interchange.file.rw
The classes and functions in this module do the writing and reading.
Premable
df_file_interchange.file.rw.chk_strict_frames_eq_ignore_nan(df1: pd.DataFrame, df2: pd.DataFrame)
Check whether two dataframes are equal, ignoring NaNs
This may be expensive since we have to make a copy of the dataframes to avoid mangling the originals. Raises exception if dfs are unequal.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
df1 |
DataFrame
|
|
required |
df2 |
DataFrame
|
|
required |
Returns:
Type | Description |
---|---|
bool
|
Always True. |
Source code in df_file_interchange/file/rw.py
df_file_interchange.file.rw.FIFileFormatEnum
df_file_interchange.file.rw.FIIndexType
Bases: str
, Enum
The type of an index, e.g. RangeIndex, Categorical, MultiIndex
Source code in df_file_interchange/file/rw.py
Encoding Specifications
Encoding options can be specified in a FIEncoding
object, which in turn contains FIEncodingCSV
and FIEncodingParquet
as attributes (only the object corresponding to the file format applies when writing). These all construct themselves with default options, it's usually ill-advised to change these.
df_file_interchange.file.rw.FIEncodingCSV
Bases: BaseModel
The parameters we use for writing or reading CSV files.
NOTE! You almost certainly do not have any reason to change these defaults. They were tested to ensure that the roundtrip write-read is exactly correct.
Attributes:
Name | Type | Description |
---|---|---|
csv_allowed_na |
list[str]
|
Default [" |
sep |
str
|
Default ",". Explictly define field separator |
na_rep |
str
|
Default " |
keep_default_na |
bool
|
Default False. |
doublequote |
bool
|
Default True. How we're escaping quotes in a str. |
quoting |
int
|
Default csv.QUOTE_NONNUMERIC. i.e. we only quote non-numeric values. |
float_precision |
Literal['high', 'legacy', 'round_trip']
|
Default "round_trip". Weirdly, Pandas's other options, including the default, don't actually return what was written with floats. |
Source code in df_file_interchange/file/rw.py
df_file_interchange.file.rw.FIEncodingParquet
Bases: BaseModel
The parameters we used for writing parquet files
Again, there's really no need to change these.
Attributes:
Name | Type | Description |
---|---|---|
engine |
str
|
Default "pyarrow". Engine to use. Has to be consistent and was tested with pyarrow |
index |
str | None
|
Default None. See https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.to_parquet.html#pandas.DataFrame.to_parquet |
Source code in df_file_interchange/file/rw.py
df_file_interchange.file.rw.FIEncoding
Bases: BaseModel
General encoding options, includes CSV and Parquet encoding
Attributes:
Name | Type | Description |
---|---|---|
csv |
FIEncodingCSV
|
Default FIEncodingCSV(). Extra options that depend on format |
parq |
FIEncodingParquet
|
Default FIEncodingParquet(). Extra options that depend on format |
auto_convert_int_to_intna |
bool
|
Default True. Whether to automatically convert standard int dtypes to Pandas's Int64Dtype (which can also encode NA values), if there are one or more NAs or None(s) in the column |
Source code in df_file_interchange/file/rw.py
Our Index Representation(s)
We have our own classes to represent Pandas indexes, which can perform operations such as serialization and instantiation (of the Pandas index). Everything here should derive from the FIBaseIndex
base class.
df_file_interchange.file.rw.FIBaseIndex
Bases: BaseModel
Base class for our custom classes to be able to serialize/deserialize/instantiate Pandas indexes
This is derived from Pydantic BaseModel
, so we can (and do) use those
facilities.
Source code in df_file_interchange/file/rw.py
index_type: str
property
Get the str name for the index (one of the FIIndex enum entires)
get_as_index(**kwargs) -> pd.Index
Creates corresponding Pandas index
Params
**kwargs : dict Not used at current time.
Returns:
Type | Description |
---|---|
Index
|
The Pandas index created corresponding to our FIIndex type and data. |
Source code in df_file_interchange/file/rw.py
df_file_interchange.file.rw.FIIndex
Bases: FIBaseIndex
Corresonds to pd.Index
See https://pandas.pydata.org/docs/reference/api/pandas.Index.html
Attributes:
Name | Type | Description |
---|---|---|
data |
ArrayLike | AnyArrayLike | list | tuple
|
The enumerated elements in the index. |
name |
str | None = None
|
Optional name. |
dtype |
Dtype | DtypeObj | ExtensionDtype | None
|
Dtype of the elemenets. |
Source code in df_file_interchange/file/rw.py
index_type: str
property
Get the str name for the index (one of the FIIndex enum entires)
get_as_index(**kwargs) -> pd.Index
Creates corresponding Pandas index
Returns:
Type | Description |
---|---|
Index
|
The Pandas index created corresponding to our FIIndex type and data. |
Source code in df_file_interchange/file/rw.py
df_file_interchange.file.rw.FIRangeIndex
Bases: FIBaseIndex
Corresonds to pd.RangeIndex
See https://pandas.pydata.org/docs/reference/api/pandas.RangeIndex.html
Attributes:
Name | Type | Description |
---|---|---|
start |
int
|
Where index starts counting from. |
stop |
int
|
Where index stops counting. |
step |
int
|
Step that index counts in. |
name |
str | None
|
Optional name. Default None. |
dtype |
DtypeObj | ExtensionDtype | str | None
|
Dtype of the index. |
Source code in df_file_interchange/file/rw.py
index_type: str
property
Get the str name for the index (one of the FIIndex enum entires)
get_as_index(**kwargs) -> pd.RangeIndex
Creates corresponding Pandas index
Returns:
Type | Description |
---|---|
RangeIndex
|
The Pandas index created corresponding to our FIIndex type and data. |
Source code in df_file_interchange/file/rw.py
df_file_interchange.file.rw.FICategoricalIndex
Bases: FIBaseIndex
Corresonds to pd.CategoricalIndex
See https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.CategoricalIndex.html
Attributes:
Name | Type | Description |
---|---|---|
data |
ArrayLike | AnyArrayLike | list | tuple
|
Elements in index. |
categories |
ArrayLike | AnyArrayLike | list | tuple
|
List from which elements in data must belong. |
ordered |
bool
|
Whether data should be ordered? |
name |
str | None
|
Optional name. Default None. |
dtype |
DtypeObj | ExtensionDtype | CategoricalDtype | str | None
|
Dtype of elements. |
Source code in df_file_interchange/file/rw.py
896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 |
|
index_type: str
property
Get the str name for the index (one of the FIIndex enum entires)
get_as_index(**kwargs) -> pd.CategoricalIndex
Creates corresponding Pandas index
Returns:
Type | Description |
---|---|
CategoricalIndex
|
The Pandas index created corresponding to our FIIndex type and data. |
Source code in df_file_interchange/file/rw.py
df_file_interchange.file.rw.FIMultiIndex
Bases: FIBaseIndex
Corresponds to pd.MultiIndex
See https://pandas.pydata.org/docs/reference/api/pandas.MultiIndex.html and https://pandas.pydata.org/docs/user_guide/advanced.html
Attributes:
Name | Type | Description |
---|---|---|
levels |
list
|
The number of levels in the multiindex. |
codes |
list
|
The list of lists (I think), of the elements in the index. |
sortorder |
int | None
|
Default None. |
names |
list
|
List of names for the levels. |
dtypes |
Series | list
|
Dtype specifications. |
Source code in df_file_interchange/file/rw.py
996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 |
|
index_type: str
property
Get the str name for the index (one of the FIIndex enum entires)
get_as_index(**kwargs) -> pd.MultiIndex
Creates corresponding Pandas index
Returns:
Type | Description |
---|---|
MultiIndex
|
The Pandas index created corresponding to our FIIndex type and data. |
Source code in df_file_interchange/file/rw.py
df_file_interchange.file.rw.FIIntervalIndex
Bases: FIBaseIndex
Corresponds to pd.IntervalIndex
See https://pandas.pydata.org/docs/reference/api/pandas.IntervalIndex.html
Attributes:
Name | Type | Description |
---|---|---|
data |
IntervalArray | ndarray
|
The data array (of intervals!). |
closed |
IntervalClosedType
|
How each interval is closed or not: "left", "right", "closed", "neither". |
name |
str or None
|
Optional name. Default None. |
dtype |
IntervalDtype | str | None
|
|
Source code in df_file_interchange/file/rw.py
1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 |
|
index_type: str
property
Get the str name for the index (one of the FIIndex enum entires)
get_as_index(**kwargs) -> pd.IntervalIndex
Creates corresponding Pandas index
Returns:
Type | Description |
---|---|
IntervalIndex
|
The Pandas index created corresponding to our FIIndex type and data. |
Source code in df_file_interchange/file/rw.py
df_file_interchange.file.rw.FIDatetimeIndex
Bases: FIBaseIndex
Corresponds to pd.DatetimeIndex
See https://pandas.pydata.org/docs/reference/api/pandas.DatetimeIndex.html
Attributes:
Name | Type | Description |
---|---|---|
data |
ArrayLike | AnyArrayLike | list | tuple
|
Array of datetimes. |
freq |
_Frequency | None = None
|
Optional frequency. See Pandas docs for what this means. |
tz |
tzinfo | str | None
|
Optional tz. |
name |
str | None = None
|
Optional name. |
dtype |
Dtype | str | None
|
|
Source code in df_file_interchange/file/rw.py
1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 |
|
index_type: str
property
Get the str name for the index (one of the FIIndex enum entires)
get_as_index(**kwargs) -> pd.DatetimeIndex
Creates corresponding Pandas index
Returns:
Type | Description |
---|---|
DatetimeIndex
|
The Pandas index created corresponding to our FIIndex type and data. |
Source code in df_file_interchange/file/rw.py
df_file_interchange.file.rw.FITimedeltaIndex
Bases: FIBaseIndex
Corresponds to pd.TimedeltaIndex
See https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.TimedeltaIndex.html
Attributes:
Name | Type | Description |
---|---|---|
data |
ArrayLike | AnyArrayLike | list | tuple
|
Array of timedeltas. |
freq |
str | BaseOffset | None = None
|
Optional frequency. See Pandas docs for details. |
name |
str | None = None
|
Optional name. |
dtype |
DtypeObj | TimeDelta64DType | Literal['<m8[ns]'] | str | None
|
|
Source code in df_file_interchange/file/rw.py
1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 |
|
index_type: str
property
Get the str name for the index (one of the FIIndex enum entires)
get_as_index(**kwargs) -> pd.TimedeltaIndex
Creates corresponding Pandas index
Returns:
Type | Description |
---|---|
TimedeltaIndex
|
The Pandas index created corresponding to our FIIndex type and data. |
Source code in df_file_interchange/file/rw.py
df_file_interchange.file.rw.FIPeriodIndex
Bases: FIBaseIndex
Corresponds to pd.PeriodIndex
See https://pandas.pydata.org/docs/reference/api/pandas.PeriodIndex.html
data: ArrayLike | AnyArrayLike | list | tuple Array of periods.
freq: _Frequency | None = None Optional frequency. See Pandas docs.
name: str | None = None Optional name
dtype: DtypeObj | pd.PeriodDtype | str | None # Hmmm.
Source code in df_file_interchange/file/rw.py
1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 |
|
index_type: str
property
Get the str name for the index (one of the FIIndex enum entires)
get_as_index(**kwargs) -> pd.PeriodIndex
Creates corresponding Pandas index
Returns:
Type | Description |
---|---|
PeriodIndex
|
The Pandas index created corresponding to our FIIndex type and data. |
Source code in df_file_interchange/file/rw.py
df_file_interchange.file.rw.FIMetainfo
Bases: BaseModel
All the collected metadata we use when saving or loading
N.B. The order of the attributes is important in the sense that the
serialization automatically preserves the order, and then yaml.dump()
does
too. This means we can make the YAML file a little easier to read/parse by a
human.
Attributes:
Name | Type | Description |
---|---|---|
datafile |
Path
|
Ironically, this should always just be the filename with no paths |
file_format |
FIFileFormatEnum
|
The file format of datafile. |
format_version |
int
|
Default 1. Not really used yet but we might need to version the YAML file. |
hash |
str | None
|
SHA256 hash of the datafile. |
encoding |
FIEncoding
|
How the datafile was or is to be encoded. |
custom_info |
SerializeAsAny[FIBaseCustomInfo]
|
Structured custom info. Can just be an empty FIBaseCustomInfo object. |
serialized_dtypes |
dict
|
Dtypes of the dataframe. |
index |
FIBaseIndex
|
Index information encoded as a FIBaseIndex object (descendent thereof). |
columns |
FIBaseIndex
|
Columns, again, specified as an FIIndex object |
Source code in df_file_interchange/file/rw.py
1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 |
|
The Write and Read Functions
These are what are exposed to the user, to roundtrip write and read dataframes.
df_file_interchange.file.rw.write_df_to_file(df: pd.DataFrame, datafile: Path | str, metafile: Path | str | None = None, file_format: FIFileFormatEnum | Literal['csv', 'parquet'] | None = None, encoding: FIEncoding | None = None, custom_info: FIBaseCustomInfo | dict = {}, preprocess_inplace=True) -> Path
Writes a dataframe to file
Parameters:
Name | Type | Description | Default |
---|---|---|---|
df |
DataFrame
|
The dataframe to save. |
required |
datafile |
Path or str
|
The datafile to save the dataframe to. |
required |
metafile |
Path or str or None(optional)
|
Metafile name, can be only the filename or with a path (which must be the same as for datafile). If not supplied or None, will be determined automatically. |
None
|
file_format |
FIFileFormatEnum | Literal['csv', 'parquet'] | None
|
The file format. If not supplied will be determined automatically. |
None
|
encoding |
FIEncoding | None
|
Datafile encoding options. |
None
|
custom_info |
FIBaseCustomInfo or dict
|
Custom user metadata to be stored. IF supplied as a FIBaseCustomInfo (or
descendent) then it stores things properly. If supplied as a dict, then
will create a FIBaseCustomInfo class and store the dictionary in the
|
{}
|
preprocess_inplace |
bool
|
|
True
|
Returns:
Type | Description |
---|---|
Path
|
A Path object with the metainfo filename in it. |
Source code in df_file_interchange/file/rw.py
2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 |
|
df_file_interchange.file.rw.write_df_to_csv(df: pd.DataFrame, datafile: Path | str, encoding: FIEncoding | None = None, custom_info: FIBaseCustomInfo | dict = {}, preprocess_inplace=True) -> Path
Simplified wrapper around write_df_to_file()
to write dataframe to CSV
Parameters:
Name | Type | Description | Default |
---|---|---|---|
df |
DataFrame
|
The dataframe. |
required |
datafile |
Path or str
|
Target datafile. |
required |
encoding |
FIEncoding | None
|
Encoding specs, can be left None for defaults. |
None
|
custom_info |
dict
|
Any custom meta data. |
{}
|
preprocess_inplace |
bool
|
Whether to do preprocessing inplace (might modify original), by default True |
True
|
Returns:
Type | Description |
---|---|
Path
|
A Path object with the metainfo filename in it. |
Source code in df_file_interchange/file/rw.py
df_file_interchange.file.rw.read_df(metafile: Path | str, strict_hash_check: bool = True, context_metainfo: dict | None = None) -> tuple[pd.DataFrame, FIMetainfo]
Load a dataframe from file
Supply the metainfo filename, not the datafilename.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
metafile |
Path
|
The YAML file that is associated with the datafile. |
required |
strict_hash_check |
bool
|
Whether we raise an exception if the hash is wrong. |
True
|
context_metainfo |
dict | None
|
If manually supplying a context to decode the structured custom info, by default None (in which was subclass type checks are used). |
None
|
Returns:
Type | Description |
---|---|
tuple[pd.DataFrame, FIMetainfo]:
|
A tuple with the dataframe and the metainfo object. |
Source code in df_file_interchange/file/rw.py
2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 |
|