Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Comments on How to resolve a "ValueError: dimension 't' already exists as a scalar variable" arising when I am using xarray.Dataset.assign_coords()?

Post

How to resolve a "ValueError: dimension 't' already exists as a scalar variable" arising when I am using xarray.Dataset.assign_coords()?

+1
−1

I have the following xarray Dataset:

d: <xarray.Dataset>
Dimensions:  (x: 79, y: 63, t: 1)
Coordinates:
  * x        (x) float64 0.9412 1.882 2.824 3.765 ... 71.53 72.47 73.41 74.35
  * y        (y) float64 59.29 58.35 57.41 56.47 ... 3.765 2.824 1.882 0.9412
  * t        (t) int32 0
Data variables:
    u        (x, y, t) float64 0.0 0.0 0.0 0.0 0.0 0.0 ... 0.0 0.0 0.0 0.0 0.0
    v        (x, y, t) float64 -0.0 -0.0 -0.0 -0.0 -0.0 ... -0.0 -0.0 -0.0 -0.0
    chc      (x, y, t) float64 1.0 1.0 1.0 1.0 1.0 1.0 ... 1.0 1.0 1.0 1.0 1.0
Attributes: (2)

When I try to assign new values to the coordinate t, using d = d.assign_coords(t = 0.123), I receive an error:

ValueError: dimension 't' already exists as a scalar variable

How can I resolve the error and assign a new value to the t coordinate?

I have tried the solutions to the similar problems posted on Stack Overflow, such as

  • assign_coords is not an inplace operation
  • recreate t as a coordinate and then assign the value to it
  • change the type of t to float
  • using dictionary: d = d.assign_coords({'t':0.123})

I suspect that the asterisk next to the name of my coordinate t has something to do with the error. But I don't understand what it means; I could only find that it is a reference to something called "proper coordinate".

History
Why does this post require moderator attention?
You might want to add some details to your flag.
Why should this post be closed?

1 comment thread

Post Feedback (2 comments)
Post Feedback
Karl Knechtel‭ wrote 9 months ago

I couldn't understand the explanation of what you tried. For example "assign_coords is not an inplace operation" doesn't say what to try, it only describes a problem. If that was the title of a post on Stack Overflow, it's not useful to just show that; we should have the links as well. Aside from that, we should have a proper example we can test; try to make a smaller Dataset that causes the problem, then show code we can use to make that Dataset ourselves from scratch.

Yes, I know all that. Correct me if I'm wrong, but it looks like you have no experience with Xarray. What you are asking me to do is like asking me to explain why 5+5=10. People with experience in Xarray will understand at once what I mean and won't have problems understanding the inplace operation reference and recreating my xarray.Dataset. Xarray is a very popular Python library. Obviously, I didn't want to bother, with my problem, those people who have never encountered Xarray. I thought it was obvious I was looking for an expert answer. That's why I didn't take time to explain every single seemingly obvious detail. I will see if I can find time to add the explanations you requested.