Geopandas read_file reads my time field with less precision. How can I make it the same as my file?

I created a file with geopandas to_file(filename, driver='GeoJSON') and it is something like this:

{ "type": "Feature", "properties": { "LineaRuta": "A", "Time": "2018-06-25 11:57:55.537" }, "geometry": { "type": "Point", "coordinates": [ 505529.0, 4788595.0 ] }

I read it with read_file(filename, driver='GeoJSON') and the time (datetime) losses its millisecond precision:

LineaRuta Time geometry 0 A 2018-06-25T11:48:09 POINT (506375 4790366)

I tried to see if it is changing the format with:

schema = gpd.io.file.infer_schema(data2) print(schema)

But the output says that it is string type. Then why it reads like a datetime? Output: {'geometry': 'Point', 'properties': OrderedDict([('LineaRuta', 'str'), ('Time', 'str')])}

I need the millisecond precision in the time field, is there any easy way to do it? For example with fiona's read?


when you write or read your file the time column is considered as a string. You can use strptime from datetime library to convert it as a datetime object

gdf = gpd.read_file(your_file, driver='GeoJSON')
gdf['date'] = gdf['your_date'].apply(lambda x: datetime.strptime(x, '%d-%m-%Y %H:%M:%S.%f'))

Here is a reference to datetime formats http://strftime.org/

  • But this is to format it to the way I want, but the information of milliseconds is already lost in read_file, right? What I would like is to read it maintaining all the info of the strings (also with milliseconds). Question edited for better understanding – Aurelie Navir Jun 25 '18 at 12:45
  • @AurelieNavir Does it behave the same way if you replace '.' by ',' for the miliseconds before saving the file ? – Hicham Zouarhi Jun 25 '18 at 13:06
  • Yes, it cuts the milliseconds too. Maybe if I change completely the format geopandas will not know? Also, there is not an easier way to define the time format reading the file? – Aurelie Navir Jun 25 '18 at 14:33
  • @AurelieNavir What if you switch to a csv file ? to force it read the time column as a string – Hicham Zouarhi Jun 26 '18 at 8:27
  • 1
    I tried to change the datime format a little bit more and it works. The format I tried: %Y_%m_%d_%H:%M:%S.%f. I guess you can also use csv format, but from what I read, it is not implemented to use directly with geopandas right? Thx for all – Aurelie Navir Jun 27 '18 at 8:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.