{"database": "github", "table": "issues", "is_view": false, "human_description_en": "where repo = 13221727, state = \"open\" and user = 6872529 sorted by updated_at descending", "rows": [[558977158, "MDU6SXNzdWU1NTg5NzcxNTg=", 3743, "A suggested solution to the `TypeError: Invalid value for attr:` error upon `.to_netcdf`", 6872529, "open", 0, null, null, 3, "2020-02-03T10:10:51Z", "2023-04-06T17:43:37Z", null, "NONE", null, null, null, "Even though netcdf conventions don't allow some data types in the attributes, it might be usefull to simply serialize those as strings rather than throw an error. Maybe add a `force_serialization` keyword argument to the `.to_netcdf` method.\r\n\r\nExample:\r\nSetup a DataArray with _bad values_ in the attributes:\r\n```python\r\nimport numpy as np\r\nfrom xarray import DataArray, load_dataset\r\nfrom pandas import Timestamp\r\n\r\nfrom numbers import Number\r\n\r\nvalid_types = (str, Number, np.ndarray, np.number, list, tuple)\r\n\r\nda = DataArray(\r\n    name='bad_values',\r\n    attrs=dict(\r\n        bool_value=True,\r\n        none_value=None,\r\n        datetime_value=Timestamp.now()\r\n    )\r\n)\r\n\r\nds = da.to_dataset()\r\nds.bad_values.attrs\r\n```\r\n\r\nOutput:\r\n```python\r\n{'bool_value': True,\r\n 'none_value': None,\r\n 'datetime_value': Timestamp('2020-02-03 10:53:02.350105')}\r\n```\r\n\r\nThe code in the except clause can be easily impolemented under [`_validate_attrs`](https://github.com/pydata/xarray/blob/4c96d53e6caa78d56b785f4edee49bbd4037a82f/xarray/backends/api.py#L185).\r\n\r\n```python\r\ntry:\r\n    ds.to_netcdf('test.nc')\r\n    # Fails with TypeError: Invalid value for attr: ...\r\nexcept TypeError as e:\r\n    print(e.__class__.__name__, e)\r\n    for variable in ds.variables.values():\r\n        for k, v in variable.attrs.items():\r\n            if not isinstance(v, valid_types) or isinstance(v, bool):\r\n                variable.attrs[k] = str(v)\r\n\r\n    ds.to_netcdf('test.nc')  # Works as expected\r\n\r\nds_from_file = load_dataset('test.nc')\r\nds_from_file.bad_values.attrs\r\n```\r\n\r\nOutput:\r\n```python\r\nTypeError Invalid value for attr: None must be a number, a string, an ndarray or a list/tuple of numbers/strings for serialization to netCDF files\r\n\r\n{'bool_value': 'True',\r\n 'none_value': 'None',\r\n 'datetime_value': '2020-02-03 10:43:38.479866'}\r\n```", "{\"url\": \"https://api.github.com/repos/pydata/xarray/issues/3743/reactions\", \"total_count\": 0, \"+1\": 0, \"-1\": 0, \"laugh\": 0, \"hooray\": 0, \"confused\": 0, \"heart\": 0, \"rocket\": 0, \"eyes\": 0}", null, null, 13221727, "issue"]], "truncated": false, "filtered_table_rows_count": 1, "expanded_columns": [], "expandable_columns": [[{"column": "repo", "other_table": "repos", "other_column": "id"}, "name"], [{"column": "milestone", "other_table": "milestones", "other_column": "id"}, "title"], [{"column": "assignee", "other_table": "users", "other_column": "id"}, "login"], [{"column": "user", "other_table": "users", "other_column": "id"}, "login"]], "columns": ["id", "node_id", "number", "title", "user", "state", "locked", "assignee", "milestone", "comments", "created_at", "updated_at", "closed_at", "author_association", "active_lock_reason", "draft", "pull_request", "body", "reactions", "performed_via_github_app", "state_reason", "repo", "type"], "primary_keys": ["id"], "units": {}, "query": {"sql": "select id, node_id, number, title, user, state, locked, assignee, milestone, comments, created_at, updated_at, closed_at, author_association, active_lock_reason, draft, pull_request, body, reactions, performed_via_github_app, state_reason, repo, type from issues where \"repo\" = :p0 and \"state\" = :p1 and \"user\" = :p2 order by updated_at desc limit 101", "params": {"p0": "13221727", "p1": "open", "p2": "6872529"}}, "facet_results": {"state": {"name": "state", "type": "column", "hideable": false, "toggle_url": "/github/issues.json?repo=13221727&state=open&user=6872529", "results": [{"value": "open", "label": "open", "count": 1, "toggle_url": "http://xarray-datasette.fly.dev/github/issues.json?repo=13221727&user=6872529", "selected": true}], "truncated": false}, "repo": {"name": "repo", "type": "column", "hideable": false, "toggle_url": "/github/issues.json?repo=13221727&state=open&user=6872529", "results": [{"value": 13221727, "label": "xarray", "count": 1, "toggle_url": "http://xarray-datasette.fly.dev/github/issues.json?state=open&user=6872529", "selected": true}], "truncated": false}, "type": {"name": "type", "type": "column", "hideable": false, "toggle_url": "/github/issues.json?repo=13221727&state=open&user=6872529", "results": [{"value": "issue", "label": "issue", "count": 1, "toggle_url": "http://xarray-datasette.fly.dev/github/issues.json?repo=13221727&state=open&user=6872529&type=issue", "selected": false}], "truncated": false}}, "suggested_facets": [{"name": "created_at", "type": "date", "toggle_url": "http://xarray-datasette.fly.dev/github/issues.json?repo=13221727&state=open&user=6872529&_facet_date=created_at"}, {"name": "updated_at", "type": "date", "toggle_url": "http://xarray-datasette.fly.dev/github/issues.json?repo=13221727&state=open&user=6872529&_facet_date=updated_at"}], "next": null, "next_url": null, "private": false, "allow_execute_sql": true, "query_ms": 73.96093099669088}