0

I have a cvs file with data that I only want to update once and if by mistake the update runs again I need to ensure the data does not update again. Before the update runs the data has no decimal places, but after the update each value is divided by 100 so each line will have 2 decimal place. My thought here is to count the number of characters after the decimal place.

This is what I want to achieve:

If >= 2 then do not update
Else if <=2 then update

I have tried to count the characters once an update has run, but some value will have 2 zero's - 120.00. When I count that I only get 1 and not 2. I need to get a 2. Essentially each updated value will have 2 decimal places even if both are zero's.

df = pd.read_csv(input.csv, names=['Open','High','Low','Close'])

df = df['Close'].astype(str).str.extract('\.(.*)').str.len()

print(df)
dps
  • 139
  • 3
  • 11

2 Answers2

0

I'm not familiar with the pd module. However, you can do something like this, where s is the column's string value:

num_decimals = len(s.partition('.')[-1])
user2340724
  • 154
  • 3
  • I misunderstood the question. I think what you actually need is this: https://stackoverflow.com/a/13384494/2340724 – user2340724 Jul 12 '19 at 15:42
0

The way you count decimals is right. It is not your fault if 120.00 == 120.0 and I don't see a way to distinguish them. So I'd say you already have an answer to your question.

However, if your question is how can you check if the file has been updated, here are suggestions depending on what your exact situation is.

If you have complete control over the function that performs update

You could add a line at the end of it that writes somewhere that update has been done:

from pathlib import Path
def update(...):
    path = Path('csv_has_been_updated.txt')
    if path.isfile():
        return  # No update is needed if the file exists
    ...
    # Perform update
    ...
    path.touch()  # Create file on disk to indicate update has been done

If you don't

You could check if the file has ever been modified (assuming that the update is the only thing that will write to the file) by comparing its creation and modification dates (see the accepted answer to this question).

Community
  • 1
  • 1
Roméo Després
  • 1,777
  • 2
  • 15
  • 30
  • Thanks this is very useful for future reference. Would you perhaps know how to then rather read a csv file in Pandas dropping the trailing zero - see post https://stackoverflow.com/questions/55503007/how-to-remove-the-0-when-reading-csv-with-pandas – dps Apr 04 '19 at 04:57