Visidata: allow use of expression for naming columns

Created on 9 Oct 2019  路  4Comments  路  Source: saulpw/visidata

I am dealing with files that don't have header with name.

If I read the documentation correctly we can create new columns using the = operator which even allows to define a python list in the visidata file and directly assign it.

However for naming columns the best option seems to be either individually name the column using the ^ operator or using one of the rows. But if all the rows are data this is not a good solution.

It is possible for me to have a mechanism to name the columns in a similar way?

Another related question. If the current column has no name is there a way to create a new column from it from applying a python function?

I did read the manual but wasn't able to find anwers for these issue.

thank you!

question

Most helpful comment

Great! You were right I was missing a conversion. Once I do it works well. :)

All 4 comments

It looks like you want the Shift+Columns Sheet. On the name column there you can transform existing names however you like, or if there are no names at all, then use gz= or other editing command to set the names.

As to your second question, there isn't currently a way to apply a function like that, you'd have to name the column to a valid identifier first. It would be pretty straightforward to make a command/plugin for creating columns like this though.

Thanks for the Shift-C and gz= suggestion it allows me to name the columns easily.

But I am still missing something to do what I actually would like.
I have put the following function in my .visidatarc:

import datetime

def d(posix_time):
  """convert posix_time to a string"""

  utc_t = datetime.datetime.utcfromtimestamp(posix_time)
  # t = utc_t.strftime('%Y-%m-%dT%H:%M:%SZ')
  t = utc_t.strftime('%dT%H:%M:%SZ')
  return t

The idea is that I have a column that is a unix time stamp and I want to be able to convert it to a more readable format.

So I thought I would be able to now use the function d but it doesn't seem to work.
If my column with the times is c1 I tried:

=d(c1)

but I only get a column filled with ! red exclamation marks.

I also have no idea how to change this column combining my new d function and for example gz=
Maybe I am missing some fundamental thing or the tool cannot do this.
Appreciate any pointers on how to do this.

In any case I found this tool after a recent podcast and I have been exploring it. I really like the concept, hope to become more familiar with it

Thank you and the people contributing to this project.

Two things will help you, I think:

  1. z Ctrl+E will show you the error message within a cell that has a ! in it as you see, and that should help you diagnose the issue with your function;
  2. @ will convert the column to date type, and if the posix timestamp in that column is already a number (like from .json, and not a string like from .csv), then the date type will convert it automatically to the proper date. Then you can put the strfime fmtstr you like into the fmtstr column on the columns sheet for that date column, and all should be as you like.

If it's not already a number, then there is a way to do it, but it's more complicated. For now your function is probably the easier way, and is just missing an int() conversion.

Great! You were right I was missing a conversion. Once I do it works well. :)

Was this page helpful?
0 / 5 - 0 ratings