Skip to content

ENH: align timedelta fractional seconds #57188

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

Open
1 of 3 tasks
wjandrea opened this issue Jan 31, 2024 · 7 comments
Open
1 of 3 tasks

ENH: align timedelta fractional seconds #57188

wjandrea opened this issue Jan 31, 2024 · 7 comments
Assignees
Labels
Enhancement Output-Formatting __repr__ of pandas objects, to_string Timedelta Timedelta data type

Comments

@wjandrea
Copy link
Contributor

wjandrea commented Jan 31, 2024

Feature Type

  • Adding new functionality to pandas
  • Changing existing functionality in pandas
  • Removing existing functionality in pandas

Problem Description

It's hard to read a column of timedeltas with fractional seconds of different precisions because it's not aligned, for example:

0             0 days 00:00:01
1      0 days 00:00:00.500000
2   0 days 00:00:00.333333333
dtype: timedelta64[ns]

It'd be easier to read like this:

0   0 days 00:00:01
1   0 days 00:00:00.500000
2   0 days 00:00:00.333333333
dtype: timedelta64[ns]

or

0   0 days 00:00:01.000000000
1   0 days 00:00:00.500000000
2   0 days 00:00:00.333333333
dtype: timedelta64[ns]

To reproduce:

s = 1 / pd.Series(np.arange(3)+1)
td = pd.to_timedelta(s, unit='s')
td

Feature Description

Add padding on the right.

In this case, it can be done by left-justifying to the largest size:

n = td.astype(str).apply(len).max()
td.astype(str).str.ljust(n)
0    0 days 00:00:01          
1    0 days 00:00:00.500000   
2    0 days 00:00:00.333333333
dtype: object

But of course, this wouldn't work for different numbers of days. For example:

td1 = pd.concat([td, pd.Series(pd.Timedelta(days=4000))])
0    0 days 00:00:01          
1    0 days 00:00:00.500000   
2    0 days 00:00:00.333333333
0    4000 days 00:00:00       
dtype: object

If timedeltas had a .replace() method so you could zero out the days, then it would work, but AFAIK such a method doesn't exist.

BTW, the same thing is already implemented for floats and datetimes.

>>> s*10
0    10.000000
1     5.000000
2     3.333333
dtype: float64
>>> pd.Timestamp(0) + td
0   1970-01-01 00:00:01.000000000
1   1970-01-01 00:00:00.500000000
2   1970-01-01 00:00:00.333333333
dtype: datetime64[ns]

Alternative Solutions

n/a

Additional Context

No response

@wjandrea wjandrea added Enhancement Needs Triage Issue that has not been reviewed by a pandas team member labels Jan 31, 2024
@rhshadrach rhshadrach added Output-Formatting __repr__ of pandas objects, to_string Timedelta Timedelta data type and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Feb 1, 2024
@rhshadrach
Copy link
Member

Thanks for the report - agreed this would be an improvement. PRs to fix are welcome.

@wjandrea
Copy link
Contributor Author

wjandrea commented Feb 2, 2024

PRs to fix are welcome.

Working on one :) but this functionality is buried in some Cython code (_Timedelta._repr_base), which I've never written before, so it's a bit of a slog.

@mafaldam
Copy link

still working on this @wjandrea ?

@mafaldam
Copy link

Hi @wjandrea , just a friendly follow-up on my previous comment. Since I haven't heard back, I wanted to let you know that I'm planning to start working on this soon. Your input would be valuable before I begin. Thanks!

@mafaldam
Copy link

take

@sofiasimass
Copy link

take

@wjandrea
Copy link
Contributor Author

@mafaldam Yeah, I actually made quite a bit of progress on this, but it's setting up the build system that I'm stuck on right now. Let me plug away at it a little while and I'll let you know. Sorry for not replying sooner :)

mafaldam added a commit to sofiasimass/pandas that referenced this issue May 27, 2024
mafaldam added a commit to sofiasimass/pandas that referenced this issue May 27, 2024
mafaldam added a commit to sofiasimass/pandas that referenced this issue May 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement Output-Formatting __repr__ of pandas objects, to_string Timedelta Timedelta data type
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants