Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fiona no longer recognizes pd.Timestamp (as datetime.datetime) since 1.10 a2 #1376

Closed
jorisvandenbossche opened this issue Apr 13, 2024 · 3 comments · Fixed by #1377
Closed
Assignees
Labels
Milestone

Comments

@jorisvandenbossche
Copy link
Member

jorisvandenbossche commented Apr 13, 2024

Example that works with released fiona, using a pd.Timestamp scalar in the data passed to writerecords for a "datetime" field:

In [1]: import fiona

In [2]: fiona.__version__
Out[2]: '1.9.6'

In [3]: schema = {'geometry': 'Point', 'properties': {'a': 'float', 'b': 'datetime'}}

In [4]: with fiona.open("test_fiona110.geojson", mode="w", schema=schema) as col:
   ...:     col.writerecords([{'id': '0', 'type': 'Feature', 'properties': {'a': 1.0, 'b': pd.Timestamp('2021-11-21 01:07:43.018000')}, 'geometry': {'type': 'Point', 'coordinates': (0.0, 0.0)}}])
   ...: 

In [5]: !cat test_fiona110.geojson
{
"type": "FeatureCollection",
"features": [
{ "type": "Feature", "properties": { "a": 1.0, "b": "2021-11-21T01:07:43.018" }, "geometry": { "type": "Point", "coordinates": [ 0.0, 0.0 ] } }
]
}

Starting with 1.10 a2, this seems to silently drop that column:

In [1]: import fiona

In [2]: fiona.__version__
Out[2]: '1.10a2'

In [3]: schema = {'geometry': 'Point', 'properties': {'a': 'float', 'b': 'datetime'}}

In [4]: with fiona.open("test_fiona110.geojson", mode="w", schema=schema) as col:
   ...:     col.writerecords([{'id': '0', 'type': 'Feature', 'properties': {'a': 1.0, 'b': pd.Timestamp('2021-11-21 01:07:43.018000')}, 'geometry': {'type': 'Point', 'coordinates': (0.0, 0.0)}}])
   ...: 

In [5]: !cat test_fiona110.geojson
{
"type": "FeatureCollection",
"name": "test_fiona110",
"features": [
{ "type": "Feature", "properties": { "a": 1.0 }, "geometry": { "type": "Point", "coordinates": [ 0.0, 0.0 ] } }
]
}

If I use datetime.datetime.fromisoformat(..) instead of pd.Timestamp(..) in the code above (i.e. passing a datetime.datetime object), then it works again.
But pd.Timestamp is subclass of datetime.datetime (and so things like isinstance(value, datetime.datetime) should work), and this worked in the past.

We are running into this issue in GeoPandas' CI because we pass the result of GeoDataFrame.iterfeatures() to writerecords. And it seems that this is creating a json-like structure that uses pd.Timestamp instead of datetime.datetime objects.

@jorisvandenbossche
Copy link
Member Author

jorisvandenbossche commented Apr 13, 2024

Enabling logging gives:

WARNING:fiona.ogrext:Skipping field b: invalid type (11, 0, 'Timestamp')

This is because of using type(value).__name__ to get the name of the type in the fieldkey lookup for the setter function (from https://github.com/Toblerity/Fiona/pull/1366/files#diff-10438bbcfe51863df4f21eb3b3ef0782ab5289899b0d408e220bf7ae43c67648R763)

Fiona/fiona/ogrext.pyx

Lines 762 to 765 in 6581da6

schema_type = session._schema_normalized_field_types[key]
fieldkey = (*FIELD_TYPES_MAP2[NAMED_FIELD_TYPES[schema_type]], type(value).__name__)
if fieldkey in self.property_setter_cache:
setter = self.property_setter_cache[fieldkey]

@sgillies sgillies self-assigned this Apr 15, 2024
@sgillies sgillies added the bug label Apr 15, 2024
@sgillies sgillies added this to the 1.10 milestone Apr 15, 2024
@sgillies
Copy link
Member

@jorisvandenbossche thanks for the report. Sorry about this, I'll make sure it's fixed for 1.10b1.

sgillies added a commit that referenced this issue Apr 16, 2024
…1377)

* Restore behavior of setting fields to values derived from date, time

Resolves #1376

* 1.10a2 release (#1368)

* This is 1.10a2

* Rename VSI plugin prefix

Resolves #1369

* This is 1.10b1.dev0

* Restore behavior of setting fields to values derived from date, time

Resolves #1376
@jorisvandenbossche
Copy link
Member Author

GeoPandas CI is green again!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants