1

How can I convert a dbf to numpy array without using arcpy?

I tried using the dbf library, but I didn't figure out how to select specific columns from my dbf to build the adequate numpy array.

Here is the script I want to reproduce without using arcpy:

arr = arcpy.da.TableToNumPyArray(inTable ,("PROVINCE","ZONE_CODE","MEAN", "Datetime","Time"))
arr = sorted(arr,key=lambda x:datetime.strptime(str(x[3]),"%d/%m/%Y %H:%M:%S"))

Using this command lines, I am able to chose the colums I want and then sort them chronologically (that's the aim of my program).

Here is the one I made with the dbf lib:

arr = dbf.Table(inTable)
arr.open()
arr = sorted(arr,key=lambda x:datetime.strptime(str(x[7]),"%d/%m/%Y %H:%M:%S"))

I don't know how to select the columns I want, and it just lasts an eternity to compile and sort.

Thank you for your help.

Nour
  • 117
  • 1
  • 2
  • 10

1 Answers1

1

One thing to note is that arr is not the same between your code snippets -- in the first it is a numpy array, and in the second it's a dbf Table.

To get what you want:

import dbf
import numpy

table = dbf.Table('some_table.dbf')
table.open()

arr = numpy.array([
    (r.province, r.zone_code, r.mean, r.datetime, r.time)
    for r in table
    ])
arr = sorted(arr,key=lambda x:datetime.strptime(str(x[3]).strip(),"%d/%m/%Y %H:%M:%S"))

I'm not sure what the difference will be in performance.


Disclosure: I am the author of the dbf package.

Nour
  • 117
  • 1
  • 2
  • 10
Ethan Furman
  • 63,992
  • 20
  • 159
  • 237
  • Oh thank you so much dear Mr Furman, I am very pleased and honored. I'll try it and give you the feedback. – Nour Jul 10 '19 at 20:28
  • I am sorry, I have an error of unconverted data: File "C:\Users\fassi\AppData\Local\Continuum\anaconda2\lib\_strptime.py", line 335, in _strptime data_string[found.end():]) ValueError: unconverted data remains I'll try to resolve it and report you the results. Thanks again =) – Nour Jul 11 '19 at 08:20
  • It's me again, sorry I went back to my code, and I found out that actually, it doesn't work that well. The numpy array returns only the fields names. I've tried to write a new script in order to append all data in the numpy array: `table = dbf.Table(inTable).open() l= len(table) k = 0 ta = [] while k – Nour Jul 12 '19 at 12:03
  • I've got my numpy array (so that answers the question) but I have an error in sorting: The error traceback: `ValueError: time data 'L' does not match format '%d/%m/%Y %H:%M:%S' ` The 'L' seems bizarre – Nour Jul 12 '19 at 12:06
  • It's all right now, that was just a stupid mistake I had. The correct script is bellow: `table = dbf.Table(inTable).open() l= len(table) k = 0 ta = [] while k – Nour Jul 12 '19 at 12:32