Package: pandavro
pandavroThe interface between Apache Avro and pandas DataFrame.Installationpandavrois available to install fromPyPI.$pipinstallpandavroDescriptionIt prepares like pandas APIs:read_avroRead the records from Avro file and fit them into pandas DataFrame usingfastavro.to_avroWrite the rows of pandas DataFrame to Avro file with the original schema infer.What can and can't pandavro do?Avro can represent the following kinds of types:Primitive types (null,bool,intetc.)Complex types (records, arrays, maps etc.)Logical types (annotated primitive/complex type to represent e.g. datetime)When converting to Avro, pandavro will try to infer the schema. It will output a non-nested schemawithout any indexesset on the dataframe and it will also not try to infer if any column can be nullable soall columns are set as nullable, i.e. a boolean will be encoded in Avro schema as['null', 'bool'].Pandavro can handle these primitive types:Numpy/pandas typeAvro primitive typenp.bool_booleannp.float32floatnp.float64doublenp.unicode_stringnp.object_stringnp.int8, np.int16, np.int32intnp.uint8, np.uint16, np.uint32"unsigned" int*np.uint64"unsigned" long*np.int64, pd.Int64Dtypelongpd.Int8Dtype, pd.Int16Dtype, pd.Int32Dtypeintpd.UInt8Dtype, pd.UInt16Dtype, pd.UInt32Dtype"unsigned" int*pd.StringDtype**stringpd.BooleanDtype**boolean* We represent the unsigned versions of these integers by adding the non-standard "unsigned" flag as such:{'type': 'int', 'unsigned': True}.  Pandas 0.24 added support for nullable integers. Writingpd.UInt64Dtypeis not supported by fastavro.** Pandas 1.0.0 added support for nullable string and boolean datatypes.Pandavro also supports these logical types:Numpy/pandas typeAvro logical typenp.datetime64, pd.DatetimeTZDtype, pd.Timestamptimestamp-micros*If a boolean column includes empty values, pandas classifies the column as having a dtype ofobject- this is accounted for in complex column handling.And these complex types - all complex types other than 'fixed' will be classified by pandas as having a dtype ofobject, so their underlying python types are used to determine the Avro type:Numpy/Python typeAvro complex typedict, collections.OrderedDictrecordlistarraynp.voidfixedRecord and array types can be arbitrarily nested within each other.The schema definition of a record requires a unique name for the record separate from the column itself. This does not map to any concept in pandas, so for this we just append '_record' to the original column name and a number to ensure that there are zero duplicate 'name' values.The remaining Avro complex types are not currently supported for the following reasons:Enum: The closest pandas type to Avro's enum type ispd.Categorical, but it still is not a complete match. Possible values of the enum type can only be alphanumeric strings, whereaspd.Categoricalvalues have no such limitation.Map: No strictly matching concept in Python/pandas - Python dictionaries can have arbitrarily typed keys. Functionality can be essentially be achieved with the record type.Union: Any column with mixed types (other than empty values/NoneType) are treated by pandas as having a dtype ofobject, and will be written as strings. It would be difficult to deterministically infer multiple allowed data types based solely on a column's contents.And these logical types:Numpy/pandas typeAvro logical typenp.datetime64, pd.DatetimeTZDtype, pd.Timestamptimestamp-micros/timezone-millisNote that the timestamp must not contain any timezone (it must be naive) because Avro does not support timezones.
Timestamps are encoded as microseconds by default, but can be encoded in milliseconds by usingtimes_as_micros=False* If passedto_avro(..., times_as_micros=False), this has a millisecond resolution.Due toan inherent design choice in fastavro, it interprets anaivedatetime in the system's timezone before serializing it. This has the consequence that yournaivedatetime will not correctly roundtrip to and from an Avro file.Always indicate a timezoneto avoid the system timezone introducing problems.If you don't want pandavro to infer the schema but instead define it yourself, pass it using theschemakwarg toto_avro.Loading Pandas nullable datatypesThe nullable datatypes indicated in the table above are easily written to Avro, but loading them introduces ambiguity as we can use either the old, default or these new datatypes. We solve this by using a special keyword when loading to force conversion to these new NA-supporting datatypes:importpandavroaspdx# Load datatypes as NA-compatible datatypes where possiblepdx.read_avro(path,na_dtypes=True)This isdifferentfromconvert_dtypesas it does not infer the datatype based on the actual values, but it looks at the Avro schema so is deterministic and not dependent on the actual values.Also note that, in "normal" mode, numpy int/uint dtypes are all read back asnp.int64due to how fastavro reads them. (This could be worked around by converting type after loading, PRs welcome.) Inna_dtypes=Truemode they are loaded correctly as Pandas NA-dtypes, but with no less than 32 bits of resolution (less is not supported by Avro so we can not infer it from the schema).ExampleSeetests/pandavro_test.pyfor more examples.importosimportnumpyasnpimportpandasaspdimportpandavroaspdxOUTPUT_PATH='{}/example.avro'.format(os.path.dirname(__file__))defmain():df=pd.DataFrame({"Boolean":[True,False,True,False],"pdBoolean":pd.Series([True,None,True,False],dtype=pd.BooleanDtype()),"Float64":np.random.randn(4),"Int64":np.random.randint(0,10,4),"pdInt64":pd.Series(list(np.random.randint(0,10,3))+[None],dtype=pd.Int64Dtype()),"String":['foo','bar','foo','bar'],"pdString":pd.Series(['foo','bar','foo',None],dtype=pd.StringDtype()),"DateTime64":[pd.Timestamp('20190101'),pd.Timestamp('20190102'),pd.Timestamp('20190103'),pd.Timestamp('20190104')]})pdx.to_avro(OUTPUT_PATH,df)saved=pdx.read_avro(OUTPUT_PATH)print(saved)if__name__=='__main__':main()