How write a dbf file

How write a dbf file from DataFrame?

There is GitHub - JuliaData/DBFTables.jl: Functions for reading dbf files in Julia, but it only supports reading at the moment, not writing.

So if you need to write DBF, your options, as far as I know, are, (1) add write support to DBFTables.jl, or (2) call into another non-julia code that can write DBF.

My main reason for working on DBFTables.jl is because DBF is used as a part of the geospatial shapefile format. GDAL knows how to write shapefiles, among many other formats. However we could make use of it, by putting some dummy geometries in a DataFrame and using GeoDataFrames.jl to write it to a shapefile. Then the other columns will end up in a DBF file.

using DataFrames, GeoDataFrames, ArchGDAL, DBFTables

point = ArchGDAL.createpoint(1.0, 2.0)  # dummy geometry
df = DataFrame(A = 1:4, B = ["M", "F", "F", "M"], geom=point)

# this doesn't only write .shp, but also .dbf
GeoDataFrames.write("path/to/file.shp", df)

# now let's see if we can read the DBF with DBFTables back to DataFrames
dbf = DBFTables.Table("path/to/test.dbf")
DataFrame(dbf)

Which gives us the original DataFrame back!

4Ɨ2 DataFrame
 Row ā”‚ A      B
     ā”‚ Int64  String
ā”€ā”€ā”€ā”€ā”€ā”¼ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€
   1 ā”‚     1  M
   2 ā”‚     2  F
   3 ā”‚     3  F
   4 ā”‚     4  M
1 Like

Sorry for reviving such an old thread but Iā€™d like to note that DBFTables now has DBFTables.write which works with any Tables.jl-compatible source.

3 Likes