# Converting Strings to number (date)

**URL:** <https://talk.observablehq.com/t/converting-strings-to-number-date/6851>\
**Category:** Help\
**Created:** [July 25, 2022, 4:14pm UTC](https://talk.observablehq.com/t/converting-strings-to-number-date/6851 "2022-07-25T16:14:14Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![AdodoolaO](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/adodoolao/32/6574_2.png) [@AdodoolaO](https://talk.observablehq.com/u/AdodoolaO)\
**Post date:** [July 25, 2022, 4:14pm UTC](https://talk.observablehq.com/t/converting-strings-to-number-date/6851/1 "2022-07-25T16:14:14Z")

</div>

Hi All! I am working on a dataset looking at Top Albums Genres over time.

The problem i’m having is converting the “rel date” from a string to number/date. I’v tried the ds.timeParse but it’s not helping.

for example I have date dating back to 1966 and its converting that date to “2066” 😅

> **[Top 5000 albums of all time (spotify)](https://observablehq.com/@adodoolao/top-5000-albums-of-all-time-spotify)**
>
> Visual ideas: Time series of best genres? albums over time? no y axis, just x axis?

I’m still getting to grips of Obserable. Your help will be much appreciated thanks 🙂

---

<div class="post-metadata">

**Author:** ![mythmon](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/mythmon/32/8504_2.png) [@mythmon](https://talk.observablehq.com/u/mythmon)\
**Post date:** [July 25, 2022, 4:54pm UTC](https://talk.observablehq.com/t/converting-strings-to-number-date/6851/2 "2022-07-25T16:54:40Z")

</div>

This is one of the major problems with two digit years. If it’s possible to get the data with a better date format, I’d recommend it. But given this data, there is something we can do.

To make this problem reasonable, we can assume that all dates are in the past 100 years. So if we find a date in the future, we simply subtract 100 years from it, and it should get back into range.

The heart of that idea is this modification to your data loading cell:

```nohighlight
top5000 = {
  let data = await FileAttachment("Top5000.csv").csv();
  let now = new Date();
  return data.map((d) => {
    let rel_date = parser(d.rel_date);
    if (rel_date > now) rel_date.setFullYear(rel_date.getFullYear() - 100);
    return { ...d, rel_date };
  });
}

```

You can see it integrated in with a full notebook here:

> **[Top 5000 albums of all time (spotify)](https://observablehq.com/d/7e1b20516a68bb1c)**
>
> Visual ideas: Time series of best genres? albums over time? no y axis, just x axis?

---

<div class="post-metadata">

**Author:** ![AdodoolaO](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/adodoolao/32/6574_2.png) [@AdodoolaO](https://talk.observablehq.com/u/AdodoolaO)\
**Post date:** [July 25, 2022, 5:54pm UTC](https://talk.observablehq.com/t/converting-strings-to-number-date/6851/3 "2022-07-25T17:54:31Z")

</div>

Thank you for all your help.

---

<div class="post-metadata">

**Author:** ![AdodoolaO](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/adodoolao/32/6574_2.png) [@AdodoolaO](https://talk.observablehq.com/u/AdodoolaO)\
**Post date:** [July 27, 2022, 7:11pm UTC](https://talk.observablehq.com/t/converting-strings-to-number-date/6851/4 "2022-07-27T19:11:40Z")

</div>

Hi Again I have another problem which i thought i sorted but I haven’t, I would love to get your help again 😅

In the data, each object has multiple genres (gen). I want to limite the “gen” to one for each artist in order to make the data easier to work with.

My solution: Turn the “gen” property string into array using the foreach & split() method. I have been successful for a small section of the data ( gens: “Conscious Hip Hop”)

But it hasn’t gone to plan when using this method for the whole data set 😓

> **[Top 5000 albums of all time (spotify)](https://observablehq.com/@adodoolao/top-5000-albums-of-all-time-spotify/2)**
>
> Visual ideas: Time series of Top genres? albums over time? no y axis, just x axis? What year did hiphop or rock have the best genre Issue is too many gens categories. Need to aggregate/group genre (sort it)

Hope this makes sense!

---

<div class="post-metadata">

**Author:** ![mythmon](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/mythmon/32/8504_2.png) [@mythmon](https://talk.observablehq.com/u/mythmon)\
**Post date:** [July 27, 2022, 7:37pm UTC](https://talk.observablehq.com/t/converting-strings-to-number-date/6851/5 "2022-07-27T19:37:59Z")

</div>

I’d recommend editing the `top5000` cell to do that work. You can modify the body of the `map` function there like this to take the first genre:

```auto
let rel_date = parser(d.rel_date);
if (rel_date > now) rel_date.setFullYear(rel_date.getFullYear() - 100);
let genre = d.gens.split(', ')[0];
return { ...d, rel_date, genre };

```

In general, I find that `forEach` is almost never what I want. It’s usually more convenient to use `map` if you want to produce a modified array, or `for-of` loops if you just want to iterate.

---

<div class="post-metadata">

**Author:** ![AdodoolaO](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/adodoolao/32/6574_2.png) [@AdodoolaO](https://talk.observablehq.com/u/AdodoolaO)\
**Post date:** [July 27, 2022, 8:56pm UTC](https://talk.observablehq.com/t/converting-strings-to-number-date/6851/6 "2022-07-27T20:56:06Z")

</div>

Thank you again!

I’m just used to loops never used map before. Looking back, using a loops doesnt really make sense, but thank you again!

---

<div class="post-metadata">

**Author:** ![AdodoolaO](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/adodoolao/32/6574_2.png) [@AdodoolaO](https://talk.observablehq.com/u/AdodoolaO)\
**Post date:** [August 15, 2022, 6:23pm UTC](https://talk.observablehq.com/t/converting-strings-to-number-date/6851/7 "2022-08-15T18:23:09Z")

</div>

Im back for the nth time 😣 😣!

It’s the same dataset. My issue is there are too many values under the “gens” column. For example, the genre “Rock” has soo many sub-genres.

How do I consolidate the data so there are clear genres without the subs. I have already started do it manually, but is there a way to fast track this process?

If not, I will continue or find an easier data set as I will have to go through 2744 columns 😅

> **[Top 5000 albums of all time (spotify)](https://observablehq.com/@adodoolao/top-5000-albums-of-all-time-spotify/2)**
>
> Visual ideas: Time series of Top genres? albums over time? no y axis, just x axis? What year did hiphop or rock have the best genre rows, columns! Starting to consolidate "gen" data one by one and adding new value back into GroupGen1 Issue is too...

Thank you very much!
