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